Deprecated
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.

If you want to play around with your scripts from within Apps Script, you’ll need to use the drive SDK. Although Drive is available as a Google Apps Script advanced service, the SDK is not – so we have to use the REST API to do stuff from.

Authentication

We need to do oAuth2. In EzyOauth2 – taking some pain out of Apps Script API authentication, I provided a library to simplify oAuth2. We’ll use that here – you should read that first to see how to set up an application on the Google Console, enable the Drive and Drive SDK API, and get some credentials. You should read up on EzyOauth2 patterns which show how to create an application that self refreshes oAuth tokens.
Here’s a very detailed step by step version of oAuth2 for Drive  – Oauth2 step by step – apps script authentication for the Drive JSON API.
Once you have done all that we can continue here.

Drive SDK

If you get this far, you’ll have published your webapp, and your properties will contain all that’s needed to generate access tokens. Our first job is to create a spreadsheet of all your standalone scripts. Since we will be writing all that to a database, in this case a spreadsheet, you’ll need a few more libraries.
info: 
{
name: "cDbAbstraction",
version: "2.0.3",
key: "MHfCjPQlweartW45xYs6hFai_d-phDA33"
}
info: 
{
name: "cUseful",
version: "2.02",
key: "Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j"
}
info: 
{
name: "cDriverSheet",
version: "2.01",
key: "Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j"
}

Click on the button below to get all keys and dependencies from the majority of my public libraries.

Now get the key of a spreadsheet to write the results to, and we are ready to go.
Running scriptExample will get or refresh an access token, and call scriptTest – this will call the drive SDK and list all script files you own.
function scriptExample () {
  // this will get an access token and pass it to doTheWork()
  return doGetPattern({} , constructConsentScreen, scriptTest,'script');
}



function scriptTest (accessToken) {
 
   var options = {
     method: "GET",
     muteHttpExceptions: true,
     headers: {
       authorization: "Bearer " + accessToken
     }
   };
   
  var url = "https://www.googleapis.com/drive/v2/files?q=" + encodeURIComponent("mimeType='application/vnd.google-apps.script' and 'me' in owners") ;

  
  var result = UrlFetchApp.fetch(url, options);
  var data = JSON.parse(result.getContentText());
  
  // open a spreadsheet as a database
  var handler = new cDbAbstraction.DbAbstraction(cDriverSheet, {
    "siloid": "scripts",
    "dbid": "1yTQFdN_O2nFb9obm7AHCTmPKpf5cwAd78uNQJiCcjPk"
  });
  if (!handler.isHappy()) throw 'could not open sheet';
  
  // remove anything already there
  var result = handler.remove();
  if (handler.handleCode < 0) throw JSON.stringify(result);
  
  // sort and log results
  var result = handler.save(data.items);
  if (handler.handleCode < 0) throw JSON.stringify(result);
  
}

That gives a massive sheet, which starts like this

Now let’s head over to Drive JSON API for apps script for a more detailed dive into cool things you can do with the SDK and your scripts

Take a look at EzyOauth2 – taking some pain out of Apps Script API authentication for more on this
For help and more information join our community, follow the blog or follow me on Twitter