This is part of the writeup on Using Google Cloud Storage with Apps Script. In Enabling APIs and OAuth2 I showed what needed to be done in the developer console to get credentials to use in your Script to access Cloud Storage. Now we'll move on to coding and how to use the service account from your script LibrariesYou'll need the cGoa library, or some other library if you are using. It can be included in your project with this key 1v_l4xN3ICa0lAW315NQEzAHPSoNiFdWHsMEwj2qA5t9cgZ5VWci2Qxv2 or the code is on github if you prefer. Once off setupThe script properties are used to automatically renew oauth2 tokens when required, so the entire OAuth2 process is invisible to you. But first, you need to populate the script Properties by running a piece of code one time. You can delete it from your project once you've run it. You need to make two changes to tailor it for you.
function oneOffSetting() { // Provoke Driveapp.getFileById() permission dialog with this comment // used by all using this script var propertyStore = PropertiesService.getScriptProperties(); // service account for cloud vision cGoa.GoaApp.setPackage ( propertyStore , cGoa.GoaApp.createServiceAccount (DriveApp , { packageName: 'xliberation-store', fileId:'0-------------0', scopes : cGoa.GoaApp.scopesGoogleExpand (['cloud-platform']), service:'google_service' })); } And that's all there is to it. Now you can delete this function and start using these credentials in your script, as described in GcsStore overview - Google Cloud Storage and Apps Script For more like this, see Google Apps Scripts snippets. Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available. |
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google cloud topics > Cloud Storage and Apps Script >