Google Apps Script Oauth2 for Vimeo with Goa

Apps Script / Oauth2 (Easy level) posted on 18th May 2018

The Vimeo Rest API is a simple, standard API. It's been added to the Goa library list of services.

Goa library is available with this key, and on github
   MZx5DzNPsYjVyZaR67xXJQai_d-phDA33

Vimeo dashboard

There are various kinds of authentication available in Vimeo. This article deals with OAuth2 - known as authenticated access by Vimeo and would be used to allow your Apps Script to access your video resources.
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. Goa will give you a redirect API that you can paste into your dashboard.

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 Vimeo 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 oneOff (){ const CLIENT_SECRET = "xxxxxxxxxxxxxxxxx"; const CLIENT_ID = "zzzzzzzzzzzzzz"; cGoa.GoaApp.setPackage (PropertiesService.getScriptProperties() , { clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, packageName: 'vimeo', scopes: ["public private"], service:'vimeo' }); }

The main points are
  • The id, account and secret come from the vimeo console
  • I've called this particular profile 'vimeo'. You can call it what you like.
  • scopes in vimeo are provided as an array of space separated scope names. Depending on your app you may want to include additional scopes
  • Vimeo access tokens dont' expire so there is no need for a refresh token process
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

Depending on what you are using it for you'll need a UI for the consent dialog. This example uses a published web app. One you've run this once and authorized it , you won't need this again. 

In the consent dialog, it will tell you the redirect URL to copy to your vimeo developer console to complete the Apps Script/Vimeo app linkage.
function doGet(e) {
  // change this to whatever store & credentials name are being used
  var goa = cGoa.GoaApp.createGoa ('vimeo',PropertiesService.getScriptProperties()).execute(e);
  
  // it's possible that we need consent - this will cause a consent dialog
  if (goa.needsConsent()) {
    return goa.getConsent();
  }
  
  // if we get here its time for your webapp to run and we should have a token, or thrown an error somewhere
  if (!goa.hasToken()) throw 'something went wrong with goa - did you check if consent was needed?';
  
  // This is a webapp doing whaever its supposed to do
  // now return it as normal
  return HtmlService.createHtmlOutput ("You're good to go and this webapp is no longer needed")
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

You can then use it in your main app like this. 
function testGoa() {
  // 
  var goa = cGoa.make('vimeo', PropertiesService.getScriptProperties());

  // check it worked
  if (!goa.hasToken()) {
    throw 'no token';
  }
  
  var url = "https://api.vimeo.com/videos/xxxxxxx";
  var result = UrlFetchApp.fetch(url, {
    headers: {
      Accept:'application/vnd.vimeo.*+json;version=3.4',
      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