In Drive JSON API for apps script I posted about an apps script library to help with the complexities of using the Drive SDK instead of the Apps Script DriveApp service.
It needs Oauth2.
Recently, the advanced Drive Service has been made available directly in Apps Script, although at the time of writing the docs are a little sketchy - they refer you to to Drive Web API which is what I'm using directly with the Drive JSON API for apps script.
In any case, unless you need some special scopes, you can fool Apps Script into getting an Auth token for you - in which case you don't need to do any of this - but if you need tokens for other services not built in to Apps Script you may as well go ahead with this.
EzyOauth2 has now been superseded by Goa, so this document is for legacy information. For more information see OAuth2 for Apps Script in a few lines of code. If you are using EzyOauth2, it's easy to migrate. If you are starting up, consider using Goa instead - it's easier and has more features.
And that's all there is to it.
Then you have to pretend that your apps script is a regular web app and go through your own Oauth dialog and manage your own tokens as described below.
Here's a recap of the tutorial on using EzyOauth2 - taking some pain out of Apps Script API authentication, which will take you though how to authenticate your app to use the drive json api.
Since there's a good chance you'll be creating an app that will not be running as a webapp, you'll need to be able to refresh tokens automatically and work offline. The EzyOauth library takes care of all that but it needs an initial set up and some local code you may want to customize.
This will take you through each step from a completely new project, including all the stuff you have to do in the cloud console.
Include the library - you'll get its details by clicking below (it uses the public library list.)
Copy the code below into a new module to your project - it doesn't need any customization, unless you want to change the property store that it uses to store your authentication information. I didn't put this in a library as I prefer to leave Properties manipulation in the hands of the script owner, but feel free to create your own from this.
Now you need to set up your app in the Google Cloud Console. There's a lot of stuff here, but it's fairly straightforward nowadays.
Name your project
Some services are enabled by default - you can disable all of them if you want
Create a new client ID
Now create this function, set your own client ID and client secret, and run it. I'll refer to this credential package from now on as 'drive', but you can call it what you like with the packageName property.
Don't change any of the other stuff. A good idea might be to make it match whatever you called your project in the cloud console. You only need to run this once. It will write these details to your chosen property store, and you can delete this function.
Even if you may be using this not as a webapp, you still need to do that once in order to allow the cloud console to recognize it and provoke an authorization dialogue. So copy this code into a new module. You don't need to change it unless you've changed the package name.
Run doGet() to get permission for the things it will need to do as a webapp. You should get a dialog like this
Now save a version, and publish as a web app and run. You'll get the dialog from your constructscreen function
You can now get an access token anytime you want using getAccessToken("drive"). To use this with Drive JSON API for apps script you would get a handle like this
Take a look at EzyOauth2 - taking some pain out of Apps Script API authentication and EzyOauth2 patterns for more on this
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > OAuth2 for Apps Script in a few lines of code > Migrating from cEzyOauth2 > Trying to like OAuth2 >