Borrowing an access token from Apps Scripts

The simplest way to get a token to use with a Google API is to borrow the one from Apps Script – assuming that you can persuade Apps Script to enter an authorization dialog for the scopes you need. This means that you can use this method for some APIS that also exist as Apps Script services, but you need to access the native APIS.
Other methods covered are
If you prefer, here’s a video version of this post. The demo source code used on all video posts are on github
All the demo code is available on github. Note that I’m using the same code for all these examples, so the final source on github will have evolved in some details from the writeup here.

 

Using the apps script token

In this example, I want to use the Google Drive API rather than the Drive or DriveApp service.
  • The first task is to see which scopes are needed for this API. The API explorer can help with this. Select the method (s) you are interested in using and click the oauth2 button. From this you can see some of the scopes you’ll need to access the API. You’ll see a dialog like this.
  • Next, you need to authorize access to this from the dev console. Since you won’t be using the Apps Script advanced drive service, you don’t need to bother with that. Just go straight to the developer console.
  • Go through the various dialogs, and eventually you’ll be allowed to enable drive. Note that you don’t need to create any credentials. The beauty of this approach is that you are going to encourage Apps Script to do all that for you.
  • Write your code, and include some reference to DriveApp – in a comment is just fine. This will provoke and authorization dialog that will get you the correct scope. Then we can just use ScriptApp.getOauthToken() to borrow the token that Apps Script is helpfully maintaining for you.
function usingAppsScriptsToken () {
   // we need to provoke a drive dialog
  // a comment is fine for that
  // DriveApp.getFiles()
   // get some files using the appssscript token
  var filesMeta = getMetaFromDriveApi ( ScriptApp.getOAuthToken() );
  // convert result
  Logger.log (JSON.stringify(filesMeta.files));
}
  • Check the scope of your project. Now you should have all the Drive scopes you need. In this example I’ve got a few more for later in these walkthroughs.

  • Use the token. This example goes to the native Drive API  and does a query. You could of course have done this in DriveApp, but now you are in a good position to use all the capabilities that are only available through the API.
function getMetaFromDriveApi (accessToken) {
  // the API end point
  var endPoint = "https://www.googleapis.com/drive/v3/files";
   // get 10 files matching some namee
  var response = UrlFetchApp.fetch (
    endPoint + "?pageSize=10&q=" + 
    encodeURIComponent(
      "name contains 'goa'" +
      " and trashed=false"
    ) + "&fields=" +
    encodeURIComponent(
      "files(id,mimeType,name),nextPageToken"
    )
    , {
    headers: {
      Authorization:'Bearer ' + accessToken
    }
  });
  // objectify the result
  return JSON.parse (response.getContentText());
}

For more like this, see Google Apps Scripts snippets