Google 本地服务广告 API

Google Local Services Ads API

我正在尝试使用节点 Google Local Services API, which is not documented yet in Google, so not sure if it's working or not yet. Note I used the PHP API as well, and I got the same results, so do not think it's related to the language. I assume it is working because of this thread

这是我正在使用的代码:

const {google} = require('googleapis');
const localservices = google.localservices('v1');

async function main() {
  //https://developers.google.com/identity/protocols/oauth2/scopes
  const auth = new google.auth.GoogleAuth({
    // Scopes can be specified either as an array
    // keyFile: './auth.json',
    // Took scopes from error:
    // 'www-authenticate': 'Bearer realm="https://accounts.google.com/", error="insufficient_scope", scope="https://www.googleapis.com/auth/adwords https://adwords.google.com/api/adwords https://adwords.google.com/api/adwords/ https://adwords.google.com/api/adwords/cm"',
    scopes: [
      'https://www.googleapis.com/auth/adwords',
      'https://adwords.google.com/api/adwords',
      'https://adwords.google.com/api/adwords/',
      'https://adwords.google.com/api/adwords/cm'
    ],
  });
  // Acquire an auth client, and bind it to all future calls
  const authClient = await auth.getClient();
  google.options({auth: authClient});

  // Do the magic
  const res = await localservices.detailedLeadReports.search({
    // Day of month. Must be from 1 to 31 and valid for the year and month, or 0 if specifying a year by itself or a year and month where the day is not significant.
    'endDate.day': 23,
    // Month of year. Must be from 1 to 12, or 0 if specifying a year without a month and day.
    'endDate.month': 9,
    // Year of date. Must be from 1 to 9999, or 0 if specifying a date without a year.
    'endDate.year': 2020,
    // The maximum number of accounts to return. If the page size is unset, page size will default to 1000. Maximum page_size is 10000. Optional.
    pageSize: 1000,
    // The `next_page_token` value returned from a previous request to SearchDetailedLeadReports that indicates where listing should continue. Optional.
    // pageToken: 'placeholder-value',
    // A query string for searching for account reports. Caller must provide a customer id of their MCC account with an associated Gaia Mint that allows read permission on their linked accounts. Search expressions are case insensitive. 
    // Example query: | Query | Description | |-------------------------|-----------------------------------------------| | manager_customer_id:123 | Get Detailed Lead Report for Manager with id | | | 123. | Required.
    query: "| Query | Description | |-------------------------|-----------------------------------------------| |manager_customer_id:111-222-3333 | Get Account Report for Manager with id 111-222-3333. |",
    // Day of month. Must be from 1 to 31 and valid for the year and month, or 0 if specifying a year by itself or a year and month where the day is not significant.
    'startDate.day': 1,
    // Month of year. Must be from 1 to 12, or 0 if specifying a year without a month and day.
    'startDate.month': 1,
    // Year of date. Must be from 1 to 9999, or 0 if specifying a date without a year.
    'startDate.year': 2020
  });
}

我收到这个错误:

  {
    ...
    code: 400,
    errors: [
      {
        message: 'Request contains an invalid argument.',
        domain: 'global',
        reason: 'badRequest'
      }
    ]
  }

我使用 this logic 获得了客户 ID。

这是一个 gist 具有完整代码的完整代码。

首先,范围需要如下所示:

  const url = oauth2Client.generateAuthUrl({
    // 'online' (default) or 'offline' (gets refresh_token)
    access_type: 'offline',
    // If you only need one scope you can pass it as a string
    // https://developers.google.com/identity/protocols/oauth2/scopes
    scope: ['https://www.googleapis.com/auth/adwords'],
  })

那么,查询将是:

query: 'manager_customer_id:XXXX'

其中 XXX 是 MCC 帐户的经理客户 ID 的数字(不含任何短划线)。看逻辑搞定here.

对于详细的报告,customer_id 也可能提供也可能不提供:

query: 'manager_customer_id:XXXX;customer_id:YYYY'