0

I am trying to get a response from Search Console REST API by using the following code, where I've already configured the OAuth2.0 authorization.

function searchConsoleQuery() {
  var service = getService();

  var apiURL = 'https://www.googleapis.com/webmasters/v3/sites/[SITE_URL]/searchAnalytics/query';

  var headers = {
    'Authorization': 'Bearer ' + service.getAccessToken(),
    'contentType':'application/json',
    'startDate':'20019-10-01',
    'endDate':'2019-10-10',
  };

  var options = {
    'payload': JSON.stringify(headers),
    'method' : 'POST',
    'muteHttpExceptions': true
  };

  var response = UrlFetchApp.fetch(apiURL, options);

  var json = JSON.parse(response.getContentText());
  Logger.log(json)

}

When I run it, the log returns the following error message:

{error={code=401, message=Login Required, errors=[{reason=required, domain=global, locationType=header, location=Authorization, message=Login Required}]}}

Please, someone help fixing this error.

1 Answer 1

4

How about this modification?

From:

var headers = {
  'Authorization': 'Bearer ' + service.getAccessToken(),
  'contentType':'application/json',
  'startDate':'20019-10-01',
  'endDate':'2019-10-10',
};

var options = {
  'payload': JSON.stringify(headers),
  'method' : 'POST',
  'muteHttpExceptions': true
};

To:

var payload = {
  'startDate':'2019-10-01',
  'endDate':'2019-10-10',
};

var options = {
  'payload': JSON.stringify(payload),
  'method' : 'POST',
  'muteHttpExceptions': true,
  'headers': {'Authorization': 'Bearer ' + service.getAccessToken()},
  'contentType':'application/json'
};

Note:

  • Please modify 20019-10-01 to 2019-10-01.
  • In this modification, it supposes as follows.
    • Your access token can be used for using Search Analytics: query of Search Console APIs API. The scope of https://www.googleapis.com/auth/webmasters.readonly and/or https://www.googleapis.com/auth/webmasters is included in the scopes for the access token.
    • You have already enabled Search Console APIs at API console.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.