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.