Goa and the Classy API

Apps Script / Oauth2 (Easy level) posted on 18th Oct 2017

The Classy Rest API uses OAuth2 a little like a Service Account, which means there's typically no user dialog. It uses a grant type called client_credentials. This is probably the most straightforward of all OAuth2 variants, so it's a breeze to both setup and use. Goa  supports this kind of OAuth2 flow, and the Classy API has been added as a service.

Goa library is available with this key.
   MZx5DzNPsYjVyZaR67xXJQai_d-phDA33


Classy dashboard

Like other APIS, there's a dashboard at which you need to create an application and get credentials. All you'll need is a client id and secret

One off function

If you're familiar with Goa, you'll know that there is a one off function you create to store your credentials, which can be deleted after running. For Classy it looks like this. Note that I'm using the Script Properties as I'm only going to use these credentials for my own script.
function oneOffClassy (){   

   cGoa.GoaApp.setPackage (PropertiesService.getScriptProperties() , {
    clientId:'Z......d',
    clientSecret:'A....dE',
    packageName: 'classy',
    service:'classy'
  });
}

The main points are
  • The id, account and secret come from the classy console
  • I've called this particular profile 'classy'. You can call it what you like.
  • I don't think there are settable scopes for this API. If there are some introduced at a later date you can add a scopes property.
Once you've run this, you're good to go and you can delete it from your script if you want. It won't be needed again.

To use

Since there is no user interaction required for this, it's very simple to initialize. Goa automatically takes care of refreshing tokens when required.
function testClassy () {
  // use it
  var goa = cGoa.make('classy', PropertiesService.getScriptProperties());

  // check it worked
  if (!goa.hasToken()) {
    throw 'no token';
  }
  
  // use it
  var result = UrlFetchApp.fetch("https://api.classy.org/2.0/organizations/4xxxx4/activity", {
    headers: {
      authorization:"Bearer " + goa.getToken()
    }
  });
  
  Logger.log (result.getContentText());
  
}



For more like this, see Google Apps Scripts snippets. Why not join our forumfollow the blog or follow me on twitter to ensure you get updates when they are available. 





Comments