Search code examples
google-apigoogle-api-php-clientgoogle-api-nodejs-client

Google Local Services Ads API


I'm trying to use the Node 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.

This is the code I'm using:

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
  });
}

And I'm getting this error:

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

I got the customer id using this logic.


Solution

  • This is a gist with the complete code that makes it work.

    First, the scopes need to look like this:

      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'],
      })
    

    Then, the query will be:

    query: 'manager_customer_id:XXXX'
    

    Where XXX is the numeric number of the manager customer ID for the MCC account (without any dashes). See logic to get it here.

    For detailed reports, customer_id may or may not be provided as well:

    query: 'manager_customer_id:XXXX;customer_id:YYYY'