Using the gplus api in Apps Script


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.

You'll notice that Google+ for domains has been implemented as an advanced service for Google Apps Script. This allows you to do lots of things with users in your domain directly from apps script. However the Google+ API is needed to play around with public G+ users. Here's how to do almost everything with the G+ API from GAS. 

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 Google+ v1 API, and get some credentials. Once you have done all that you can set up your credentials in your app with a one time function like this.

  setAuthenticationPackage_ ({ 
    clientId : "xxxxxx.apps.googleusercontent.com",
    clientSecret : "xxxxxxx",
    scopes : [
      'https://www.googleapis.com/auth/plus.login',
      'https://www.googleapis.com/auth/plus.me',
      'https://www.googleapis.com/auth/userinfo.email',
      'https://www.googleapis.com/auth/userinfo.profile'
    ],
    service: 'google',
    packageName: 'xliberationgplus'
  });

You'll then need to set up your redirect URI and run your doGet() once to get authorized as described in EzyOauth2 - taking some pain out of Apps Script API authentication. You'll remember from there that we only need to run that once, since a refresh token is stored in your properties and automatically gets a fresh access token when needed. 

Here's your one time doGet() pattern that you need to publish run and add the redirectURI to your cloud console credentials. It's virtually the same as the one used for the datastore example in EzyOauth2 patterns, except it references the credentials you set up for G+ with setAuthenticationPackage.


Here's your one time doGet() pattern.
/** 
 * this is your web app
 * @param {object} webapp param object
 * return {HtmlOutput} 
 */

function doGet (e) {
  return doGetPattern(e, constructConsentScreen, doSomething, 'xliberationgplus') ;
}

/**
 * tailor your consent screen with an html template
 * @param {string} consentUrl the url to click to provide user consent
 * @return {string} the html for the consent screen
 */
function constructConsentScreen (consentUrl) {
  return '<a href = "' + consentUrl + '">Authenticate to gplus</a> ';
}

/**
 * this is your main processing - will be called with your access token
 * @param {string} accessToken - the accessToken
 */
function doSomething (accessToken) {
 
   var options = {
     method: "GET",
     headers: {
       authorization: "Bearer " + accessToken
     }
   };

  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/me", options);
  return HtmlService.createHtmlOutput (' it worked' + result.getContentText());

}

/**
 * gets the property key against which you want the authentication package stored
 * @param {string} optPackageName
 * @return {string}
 */
function getPropertyKey_ (optPackageName) {
  
  return "EzyOauth2Datastore" + (optPackageName ? '_' + optPackageName : '');
}

G+ API.

Here's an example of pretty much everything you can do with the API. I haven't added 'moments' to this - that'll be the subject of a different article. 

function gplusExample () {
  // this will get an access token and pass it to doTheWork()
  return doGetPattern({} , constructConsentScreen, gplusTest,'xliberationgplus');
}

function gplusTest (accessToken) {
 
   var options = {
     method: "GET",
     muteHttpExceptions: true,
     headers: {
       authorization: "Bearer " + accessToken
     }
   };

  // get my own profile
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/me", options);
  Logger.log (result.getContentText());
  
  // get all people matching the query
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people?query=Google", options);
  Logger.log (result.getContentText());
  var rob = JSON.parse(result.getContentText());
  
  // get details on each of those people
  rob.items.forEach ( function (d) {
    var r2 = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/" + d.id, options);
    Logger.log (r2.getContentText());
  });
  
  // get all the people visible to me (depending on what was authorized when authentication was done)
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/me/people/visible", options);
  Logger.log (result.getContentText());
  
  // get all my activities
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/me/activities/public", options);
  Logger.log (result.getContentText());
  var rob = JSON.parse(result.getContentText());
  
  // get a particular activity
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/activities/" + rob.items[0].id, options);
  Logger.log (result.getContentText());
  
  // get all the comments on a particular activity
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/activities/" + rob.items[0].id +"/comments", options);
  Logger.log (result.getContentText());
  var rob = JSON.parse(result.getContentText());
  
  // get particular comments
  if (rob.items.length) {
    var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/comments/" + rob.items[0].id, options);
    Logger.log (result.getContentText());
  }
  
  // search for some activities
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/activities?query=google apps script", options);
  Logger.log (result.getContentText());
  var rob = JSON.parse(result.getContentText());
  
  // get profile about the person who posted the 1st query result
  var result = UrlFetchApp.fetch("https://www.googleapis.com/plus/v1/people/" + rob.items[0].actor.id, options);
  Logger.log (result.getContentText());


}

Comments