0

The following Apps Script code in a Chrome extension does not error, but return no calendars:

chrome.identity.getAuthToken({interactive: true}, function(token) 
{
  var init = 
  {
    method: 'GET',
    async: true,
    headers: 
    {
      Authorization: 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
    'contentType': 'json'
  };

  fetch('https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=writer&key=' + apiKey, init)
    .then(function(data)
    {
      log('Object ' + JSON.stringify(data));
    });
});

I just get this in the console: Object {}

I saw this post and tried the suggestions, no luck. The apiKey came from the API console, and works in a different call. Thanks for any tips or pointers.

Edit #2: Here is my current manifest.json:

"oauth2": {
    "client_id": "123456780416-17ur6mlc88sfot8e4s2pq05ehtkd8klh.apps.googleusercontent.com",
"scopes":[
           "https://www.googleapis.com/auth/script.external_request",
           "https://www.googleapis.com/auth/contacts.readonly",
           "https://www.googleapis.com/auth/calendar"
         ]
}
Scott
  • 477
  • 4
  • 20

1 Answers1

0

If you are passing a token into the options of the fetch request, you do not need an API key.

Modify

fetch('https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=writer&key=' + apiKey, init)

to fetch('https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=writer', init)

and define init as

  var init = {
    "method":"get",
    "async": true,
    "muteHttpExceptions": true,
    "headers": {
      "Authorization": "Bearer " + token
    },
    "contentType": "application/json", 
  }

Also make sure that your token contains the necessary scopes.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Tried that fetch and init, same result. Also, I edited my question to add the manifest.json to show scopes. – Scott Oct 22 '19 at 16:19
  • What about "https://www.googleapis.com/auth/script.external_request"? Also, if you use Apps Script, include `response = UrlFetchApp.fetch(url,init); response = JSON.parse(response); Logger.log('response ' + JSON.stringify(response))`to log the error code if any – ziganotschka Oct 22 '19 at 18:08
  • I added the script.external_request scope, but it is somehow not pulling it in as I am getting this error: UrlFetchApp is not defined What do you mean by my token containing the necessary scopes? – Scott Oct 22 '19 at 20:53
  • How do you get your token? With `ScriptApp.getOAuthToken()`? Or with a service account? `UrlFetchApp ` is an Apps Script class. I am not very familiar with Chrome extensions, but it seems like you should be able to run regular Apps Script code: https://stackoverflow.com/questions/18404922/can-i-execute-google-apps-script-code-from-a-chrome-extension – ziganotschka Oct 23 '19 at 07:02
  • I am not using AppScript, just the Google REST API. I think the key to this might be your statement about making sure my token has the necessary scopes. After I added the Calendar scope in my manifest do I have to do something with the token to let it know about this change? – Scott Oct 25 '19 at 15:39