google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

help with working example webmasters.searchanalytics.query

Open VorticonCmdr opened this issue 11 years ago • 6 comments

Hi,

I looks like I can't get webmasters.searchanalytics.query working. Anyone willing and able to provide a working example?

Regards Valentin

VorticonCmdr avatar Aug 06 '15 07:08 VorticonCmdr

Hi,

Just follow: https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js

Use this scope when creating authClient: https://www.googleapis.com/auth/webmasters.readonly

Instead of drive api:

var drive = google.drive('v2');

get the webmaster api

var webmaster = google.webmasters('v3');

Instead of

drive.files.list({ auth: authClient }, function(err, resp) {
    // handle err and response
  });

Call the searchanalytics

var params = {
    auth: authClient,
    siteUrl:'http://www.example.com',
    resource : {
      startDate: '2015-08-25',
      endDate: '2015-08-25',
      dimensions:['query', 'page'],
      rowLimit:10
    },
    aggregationType: 'byPage'
};
  webmaster.searchanalytics.query(params, function(err, resp) {
    console.log(resp);
    console.log(err);
  });

The key time consumer for me was to setup the params object. Now its all a breeze.

prakashbande avatar Sep 01 '15 17:09 prakashbande

@prakashbande can you be a bit more specific on how you setup this part?

var authClient = new google.auth.JWT(
    '[email protected]',
    'path/to/key.pem',
    // Contents of private_key.pem if you want to load the pem file yourself
    // (do not use the path parameter above if using this param)
    'key',
    // Scopes can be specified either as an array or as a single, space-delimited string
    ['https://www.googleapis.com/auth/drive.readonly'],
    // User to impersonate (leave empty if no impersonation needed)
    '[email protected]');

This example is not concrete enough to avoid confusion. For example, to use subject account email or not, and if that is client id email or the actual user's google account. Without the last email parameters I get Error: User does not have sufficient permission for site. With it, I get invalid request. I do think I had parameters wrong, so your example with definitely help.

rothnic avatar Sep 26 '15 14:09 rothnic

@rothnic In your example you're using the wrong scope. You need https://www.googleapis.com/auth/webmasters.readonly

jmdobry avatar Mar 04 '16 06:03 jmdobry

@VorticonCmdr Does @prakashbande's example work for you?

jmdobry avatar Mar 04 '16 06:03 jmdobry

Closing as this does not appear to be an issue. If you are convinced that this is really a bug, please feel free to re-open the issue and add more information such as:

  • Expected behavior
  • Actual behavior
  • Code samples
  • Version numbers
  • References docs or other links

Or, consider opening a Pull Request.

Otherwise, support questions are handled on Stack Overflow. Ask a question on Stackoverflow

jmdobry avatar Mar 05 '16 00:03 jmdobry

Updated code sample found at: https://github.com/google/google-api-nodejs-client/blob/master/samples/jwt.js

CarsonBanov avatar Oct 24 '16 19:10 CarsonBanov