Using the Service Account
If you prefer, there is a summary video version of this post here
Creating a project
Enabling the API
Getting credentials
Create a new service account
Give it a name and download the JSON file
Find out what scopes are needed
Include cGoa
You’ll need the cGoa library to manage the interactions with the Google token infrastructure. It’s here
MZx5DzNPsYjVyZaR67xXJQai_d-phDA33
or if you prefer, you can get the source from github.
Set up one off function to store your credentials.
function oneOffSetting() { // used by all using this script var propertyStore = PropertiesService.getScriptProperties(); // service account for cloud vision cGoa.GoaApp.setPackage (propertyStore , cGoa.GoaApp.createServiceAccount (DriveApp , { packageName: 'cloudvision_serviceaccount', fileId:'0B92ExxxxxxxWXkzNXM', scopes : cGoa.GoaApp.scopesGoogleExpand (['cloud-platform']), service:'google_service' })); }
The example
Pets
Faces
Getting the service token
/** * demonstrate using service account * getting/refreshing service token is pretty much a one liner */ function usingServiceAccount () { logVision (Goth.getToken('cloudvision_serviceaccount')); }
For simplicity I’ve centralized the getting of tokens to this namespace in this project. I recommend you do the same, as you’ll see when we come to implement the web Oauth2 flow in future examples. Like this, you don’t need to worry about the mechanics of how Goa talks to the OAUTH2 infrastucture.
/** * namespace to deal with oauth * @namespace Oauth */ var Goth = (function(ns) { /** * get a goa or fail * @param {string} packageName the package name * @param {PropertiesService} [props=ScriptProperties] * @return {Goa} a goa */ ns.getGoa = function (packageName,props) { return cGoa.GoaApp.createGoa( packageName, props || PropertiesService.getScriptProperties() ).execute(); } /** * get a token or fail * @param {string} packageName the package name * @param {PropertiesService} [props=ScriptProperties] * @return {Goa} a goa */ ns.getToken = function (packageName,props) { var goa = ns.getGoa (packageName, props); if (!goa.hasToken()) { throw 'no token retrieved'; } return goa.getToken(); } return ns; }) (Goth || {});
Using the Cloudvision API
/** * log out the result of both tests * @param {string} accessToken the access token */ function logVision (accessToken) { // i have some animal images in this folder // findout what they are of var result = Pull.pets (accessToken); Logger.log ( JSON.stringify(result.map(function(d) { return { name:d.file.fileName, annotation:d.annotation }; }))); // some faces var result = Pull.faces (accessToken); // just summarize Logger.log ( JSON.stringify(result.map(function(d) { return { name:d.file.fileName, annotation:d.annotation }; }))); } function cloudVisionAnnonate (accessToken, folderId , type, maxResults) { // the API end point var endPoint = "https://vision.googleapis.com/v1/images:annotate"; // get the images and encode them var folder = DriveApp.getFolderById(folderId); if (!folder) { throw 'cant find image folder ' + folderId; } // get all the files in the folder var iterator = folder.searchFiles("mimeType contains 'image/'"); var files = []; while (iterator.hasNext()) { var file = iterator.next(); files.push({ fileName:file.getName(), b64:Utilities.base64Encode(file.getBlob().getBytes()), type:file.getMimeType(), id:file.getId() }); } if (!files.length) { Logger.log ('couldnt find any images in folder ' + folder.getName()); } // now create the post body maxResults = maxResults || 1; type = type || "LABEL_DETECTION" var body = { requests: files.map (function (d) { return { features: [{ "type":type || "LABEL_DETECTION", "maxResults":maxResults }], image: { content: d.b64 } } })}; // can cost money so use cache var cache = CacheService.getScriptCache(); var cacheKey = files.map(function(d) { return d.id; }) .concat ([type,maxResults]) .join("_") var cacheData = cache.get(cacheKey); var result; if (!cacheData) { // do the cloud vision request var response = UrlFetchApp.fetch ( endPoint, { method: "POST", payload: JSON.stringify(body), contentType: "application/json", headers: { Authorization:'Bearer ' + accessToken } }); // objectify the result result = JSON.parse(response.getContentText()); cache.put (cacheKey , response.getContentText()); } else { result = JSON.parse(cacheData); } return files.map (function (d,i) { return { file:d, annotation:result.responses[i] } }); }
I’ve centralized which folders to pull the images from for this example into this namespace. As this is developed in future examples, this will be replaced by a filepicker dialog.
var Pull = (function (ns) { /** * do the pets image analysis * @param {string} accessToken the accessToken * @param {string} folderId the folder id with the images * @return {object} the pets result */ ns.pets = function (accessToken,folderId) { return cloudVisionAnnonate ( accessToken, folderId || '0B92ExLh4POiZYzZZT3d0aU9VV1U', 'LABEL_DETECTION' , 3 ); }; /** * do the faces image analysis * @param {string} accessToken the accessToken * @return {object} the pets result */ ns.faces = function (accessToken,folderId) { // some people images here return cloudVisionAnnonate ( accessToken, folderId || '0B92ExLh4POiZZDNKcVN0QTJJMGs', 'FACE_DETECTION', 1 ); }; return ns; })({});
I’ll be covering how to render this stuff using html service in a later post, but for now here’s a very small snippet of the data returned by these functions.
[{ "name": "terrier.jpg", "annotation": { "labelAnnotations": [{ "mid": "/m/068hy", "description": "pet", "score": 0.99145305 }, { "mid": "/m/0bt9lr", "description": "dog", "score": 0.9912262 }, { "mid": "/m/04rky", "description": "mammal", "score": 0.96640766 }] } }, { "name": "cute-dogs.jpg", "annotation": { "labelAnnotations": [{ "mid": "/m/068hy", "description": "pet", "score": 0.98858243 }, { "mid": "/m/0bt9lr", "description": "dog", "score": 0.98743832 }, { "mid": "/m/04rky", "description": "mammal", "score": 0.96527207 }] } }, { "name": "stuffed.jpg", "annotation": { "labelAnnotations": [{ "mid": "/m/0jbk", "description": "animal", "score": 0.80946273 }, { "mid": "/m/0kmg4", "description": "teddy bear", "score": 0.71301168 }, { "mid": "/m/0138tl", "description": "toy", "score": 0.60209852 }] } }] [{ "name": "frumpy.jpg", "annotation": { "faceAnnotations": [{ "boundingPoly": { "vertices": [{ "x": 196 }, { "x": 490 }, { "x": 490, "y": 339 }, { "x": 196, "y": 339 }] }, "fdBoundingPoly": { "vertices": [{ "x": 229, "y": 88 }, { "x": 445, "y": 88 }, { "x": 445, "y": 304 }, { "x": 229, "y": 304 }] }, "landmarks": [{ "type": "LEFT_EYE", "position": { "x": 284.49878, etc.............
- 2 ways to create and preserve formulas with fiddler for Google Apps Script
- A fourth way to preserve and create formulas with Fiddler for Sheets, plus some more new methods
- A functional approach to updating master sheet with Fiddler
- A recursive extend function for Apps Script
- A third way to preserve formulas with fiddler, plus 2 new methods
- A webapp to share copies of the contents of folders
- Abstracting services with closures
- Add-on spinner
- Addressing namespace and library methods from google.script.run
- Anonymous user registration with the Apps Script PropertiesService
- Apps for Office – binding example comparison
- Apps Script as a proxy
- Apps Script const scoping problems
- Apps Script server side to client side htmlservice progress reporting using CacheService
- Calculating image dimensions in server side apps script
- Calculating the last day of a given weekday in the month
- Canvasser
- Chaining JavaScript
- Changing class properties dynamically
- Checking the argument types in Apps Script
- Cleaning up a document format
- Cleaning up heading levels
- Column numbers to characters
- Composing functions and functional programming
- Configurable canvas meter
- Convert JSON to XML
- Converting SVG to PNG with JavaScript
- Converting timestamps to dates formula
- Copying canvas and svg images from your Add-on
- Copying to new host location
- Copying to new host location
- Counting script and library usage
- Create sha1 signatures with apps script
- Creating a key digest to use for a cache key or to compare content
- Creating a pile of files list from Google Drive
- Creating and working with transposed sheet data arrays
- Cross Origin Resource sharing (CORS)
- CryptoJS libraries for Google Apps Script
- Custom checking for exponential backoff
- Data wrangling with named columns in Google Spreadsheet
- Dealing with objects that are too large for the property or cache store
- Detecting Spreadsheet tables automatically with Google Apps Script
- Direction minimizer – other usages
- Do something useful with GAS in 5 minutes
- Dynamically creating tables with clusterize.js
- EasyCron Library
- ES6 JavaScript features
- Exponential backoff
- Exponential backoff for promises
- Fiddler – A functional approach to fiddling with sheet data
- Fiddler and rangeLists
- Fiddler now supports joins to merge matching columns from multiple sheets
- Fiddling with text fields that look like dates
- Filling ranges in Google Sheets
- Finding a Drive App folder by path
- Finding where Drive hosting is being used in Sites
- Flattening an object with dot syntax
- Flattening and unflattening objects to spreadsheets
- Formatting GraphQL queries
- Formatting sheet column data with fiddler
- From notes to frequencies and back again
- From Xml to JSON
- Generating and managing random lists with JavaScript and Apps Script
- Generating coupon codes with expiry dates
- Generating test data for sheets and tables
- Get GAS library info
- Getting an htmlservice template from a library
- Getting insights into Sheets performance
- Google Drive as cache
- Header formatting with fiddler
- Highlight duplicate rows in a sheet – map and reduce
- Highlight duplicate rows in a sheet – map, filter and every
- How to determine what kind of object something is in Apps Script
- How to get stats about youtube videos in your channel with apps script
- How to pass non stringifyable objects to html service
- How to transpose spreadsheet data with apps script
- Identify duplicates on Drive
- Identifying hosted files
- Implementing a client side progress bar reporting on server side work for htmlService
- Importing Predictwise data
- Improved namespace pattern for Apps Script
- Including the stack in custom errors
- JavaScript closures – how, where and why
- JavaScript currying and functional programming
- JavaScript currying and functional programming – even more
- JavaScript recursion primer
- JavaScript snippet to create a default method for an object
- JSONP and JSON and Google Apps Script webapps
- Loading large JSON datasets into BigQuery with Apps Script
- Logging differences in strings in Apps Script
- Measuring library load speed
- Migrating user and script properties
- Minimizing maps directionfinder api calls
- More client server code sharing
- More recursion – parents and children
- More sheet data fiddling
- Multiple inserts in Fusion Tables
- Namespaces in libraries and scripts
- Normalizing the header level of blank paragraphs
- Optimizing sheet formatting with Apps Script
- Optimizing showing and hiding rows and columns
- Organizing asynchronous calls to google.script.run
- Organizing parallel streams of server calls with google.script.run promises
- Parallel process orchestration with HtmlService
- Passing data to html service
- Patching site html
- Populating sheets with API data using a Fiddler
- Proxy jsonp
- Pseudo binding in HTML service
- Queuing asynchronous tasks with rate limit and concurrency constraints
- Random and fake test data in Sheets with Google Apps Script
- Recursive async functions
- Removing duplicate paragraphs
- Reporting file, function and line number in Apps Script
- Resumable uploads – writing large files to Drive with Apps Script
- Reusing html stuff between Apps Script projects
- Roughly matching text
- Serving apps script to JavaScript app
- Sharing code between client and server
- Shortcut for adding nested properties to a JavaScript object
- Simple server side polling
- Sorting Google Sheet DisplayValues
- Squeezing more into (and getting more out of) Cache services
- Styling Gmail html tables
- Summarizing emails to a sheet
- SunCalc
- Super simple cipher library for Apps Script encryption and decryption
- TimeSimmer : An adjustable timer for apps that need to speed up or slow down time
- Transform dates for add-on transfer
- Transposing sheet data
- Traversing a tree
- Unique values with data fiddler
- Unnesting data to sheet values
- Untangling with promises
- Use Drive properties to find app files
- Use promise instead of callback for settimeout
- Using Advanced Drive service to convert files
- Using Apps Script for xml json conversion
- Using array formulas to improve performance
- Using crossfilter with Google Apps Script
- Using D3 in server side Gas
- Using es6 promises server side in Apps Script
- Using Es6 with Apps Script
- Using exponential backoff with github api – dealing with data “in preparation”
- Using Google sheets via Bigquery from Apps Script
- Using named locks with Google Apps Scripts
- Using promises to orchestrate Html service polling
- Using promises with apps script
- Using the Itunes API with Apps Script
- Using the slideshare API from Apps Script
- Using timing functions to get insight into Sheets
- Watching docs for changes
- Watching for server side changes from the client html service
- What JavaScript engine is Apps Script running on?
- Why Base64
- Zipping to make stuff fit in cache or properties service.
Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.