Migrating from cEzyOauth2

This describes how to migrate from cEzyOauth2 while using the Goa library as described in Oauth2 for Apps Script in a few lines of code (which you should read first for background).

The library, cGoa, is available under this project key.

MZx5DzNPsYjVyZaR67xXJQai_d-phDA33

Compatibility

Goa is compatible with cEzyOauth2, and can use existing credentials and tokens that have been created by cEzyOauth2. The redirect URI will have changed and will need to be updated in the app dashboard as advised by the consent dialog when you get one.

Patterns


The patterns for Goa are much simpler. I converted gasGit, which needs 2 token flows (one for drive, another for github)  in a few minutes. There was no need to reinitialize the credentials since they are the same structure. After deleting all the ezyoauth2 stuff,  the doGet pattern became. For more details on multiple Oauth2 see Multiple oAuth2 authentication packages

function doGet(e) {

  // i need these two
  var fs = ['script','gasgit'] ;

  
  // if we are being called back to get consent then the name of the package will be in the parameters
  var name = cGoa.GoaApp.getName(e);
  if(name) {
    var goa = cGoa.GoaApp.createGoa(name,PropertiesService.getScriptProperties()).execute(e); 
    // renter for consent
    if (goa.needsConsent()) {
      return goa.getConsent();
    }
  }
  
  // if we get here then we look through each one to see if any more consent is needed
  for (var i = 0; i < fs.length ; i++ ) {
    var goa = cGoa.GoaApp.createGoa(fs[i],PropertiesService.getScriptProperties()).execute(); 
    if (goa.needsConsent()) {
      return goa.getConsent();
    }
    if (!goa.hasToken()) throw 'something went wrong with goa - did you check if consent was needed?';
  }
  
  
   return HtmlService.createHtmlOutput ('all tokens created for each of ' + fs.join(','))
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

Next I needed a new version of getAccessToken() which I had written to pick up tokens inside the gasGit code.
 
function getAccessToken(packageName) {
  return  cGoa.GoaApp.createGoa(packageName , PropertiesService.getScriptProperties() ).execute().getToken();
}

Finally, I rewrote the the setOneTimeProperties - although not needed for this, they may be in the future. Once I run these and provoke a consent dialog, I'll need to update the redirect URI in the dev console.

function oneTimeSetProperties () {

  cGoa.GoaApp.setPackage (propertyStore , { 
    clientId : "xxx.apps.googleusercontent.com",
    clientSecret : "CH1l3wW6lZofc0jX2OR6iBvi",
    scopes : cGoa.GoaApp.scopesGoogleExpand (['drive','drive.scripts']),
    service: 'google',
    packageName: 'script'
  });

  cGoa.GoaApp.setPackage (propertyStore , { 
    clientId : "xxx",
    clientSecret : "xxx",
    scopes : [
      'gist',
      'repo'
    ],
    service: 'github',
    packageName: 'gasgit'
  });

}

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'ReillyAmazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




For more like this, see OAuth2 for Apps Script in a few lines of code. Why not join our forumfollow the blog or follow me on twitter to ensure you get updates when they are available. 




Subpages (1): Trying to like OAuth2
Comments