help with working example webmasters.searchanalytics.query
Hi,
I looks like I can't get webmasters.searchanalytics.query working. Anyone willing and able to provide a working example?
Regards Valentin
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 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 In your example you're using the wrong scope. You need https://www.googleapis.com/auth/webmasters.readonly
@VorticonCmdr Does @prakashbande's example work for you?
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. 
Updated code sample found at: https://github.com/google/google-api-nodejs-client/blob/master/samples/jwt.js