function GoogleApis(options) {
this.options(options);
this.addAPIs(apis);
/**
* A reference to an instance of GoogleAuth.
*
* @name GoogleApis#auth
* @type {GoogleAuth}
*/
this.auth = new GoogleAuth();
/**
* A reference to the {@link GoogleApis} constructor function.
*
* @name GoogleApis#GoogleApis
* @see GoogleApis
* @type {Function}
*/
this.GoogleApis = GoogleApis;
}
n/a
acceleratedmobilepageurl = function () { [native code] }
...
* Accelerated Mobile Pages (AMP) URL API
*
* This API contains a single method, batchGet. Call this method to retrieve the AMP URL (and equivalent AMP Cache URL) for given
public URL(s).
*
* @example
* var google = require('googleapis');
* var acceleratedmobilepageurl = google.acceleratedmobilepageurl('v1');
*
* @namespace acceleratedmobilepageurl
* @type {Function}
* @version v1
* @variation v1
* @param {object=} options Options for Acceleratedmobilepageurl
*/
...
adexchangebuyer = function () { [native code] }
...
/**
* Ad Exchange Buyer API
*
* Accesses your bidding-account information, submits creatives for validation, finds available direct deals, and retrieves performance
reports.
*
* @example
* var google = require('googleapis');
* var adexchangebuyer = google.adexchangebuyer('v1.2');
*
* @namespace adexchangebuyer
* @type {Function}
* @version v1.2
* @variation v1.2
* @param {object=} options Options for Adexchangebuyer
*/
...
adexchangebuyer2 = function () { [native code] }
...
/**
* Ad Exchange Buyer API II
*
* Accesses the latest features for managing Ad Exchange accounts, Real-Time Bidding configurations and auction metrics, and Marketplace
programmatic deals.
*
* @example
* var google = require('googleapis');
* var adexchangebuyer2 = google.adexchangebuyer2('v2beta1');
*
* @namespace adexchangebuyer2
* @type {Function}
* @version v2beta1
* @variation v2beta1
* @param {object=} options Options for Adexchangebuyer2
*/
...
adexchangeseller = function () { [native code] }
...
/**
* Ad Exchange Seller API
*
* Accesses the inventory of Ad Exchange seller users and generates reports.
*
* @example
* var google = require('googleapis');
* var adexchangeseller = google.adexchangeseller('v1.1');
*
* @namespace adexchangeseller
* @type {Function}
* @version v1.1
* @variation v1.1
* @param {object=} options Options for Adexchangeseller
*/
...
admin = function () { [native code] }
...
/**
* Admin Data Transfer API
*
* Transfers user data from one user to another.
*
* @example
* var google = require('googleapis');
* var admin = google.admin('datatransfer_v1');
*
* @namespace admin
* @type {Function}
* @version datatransfer_v1
* @variation datatransfer_v1
* @param {object=} options Options for Admin
*/
...
adsense = function () { [native code] }
n/a
adsensehost = function () { [native code] }
...
/**
* AdSense Host API
*
* Generates performance reports, generates ad codes, and provides publisher management capabilities for AdSense Hosts.
*
* @example
* var google = require('googleapis');
* var adsensehost = google.adsensehost('v4.1');
*
* @namespace adsensehost
* @type {Function}
* @version v4.1
* @variation v4.1
* @param {object=} options Options for Adsensehost
*/
...
analytics = function () { [native code] }
...
/**
* Google Analytics API
*
* Views and manages your Google Analytics data.
*
* @example
* var google = require('googleapis');
* var analytics = google.analytics('v2.4');
*
* @namespace analytics
* @type {Function}
* @version v2.4
* @variation v2.4
* @param {object=} options Options for Analytics
*/
...
analyticsreporting = function () { [native code] }
...
/**
* Google Analytics Reporting API
*
* Accesses Analytics report data.
*
* @example
* var google = require('googleapis');
* var analyticsreporting = google.analyticsreporting('v4');
*
* @namespace analyticsreporting
* @type {Function}
* @version v4
* @variation v4
* @param {object=} options Options for Analyticsreporting
*/
...
androidenterprise = function () { [native code] }
n/a
androidpublisher = function () { [native code] }
...
/**
* Google Play Developer API
*
* Lets Android application developers access their Google Play accounts.
*
* @example
* var google = require('googleapis');
* var androidpublisher = google.androidpublisher('v2');
*
* @namespace androidpublisher
* @type {Function}
* @version v2
* @variation v2
* @param {object=} options Options for Androidpublisher
*/
...
appengine = function () { [native code] }
...
/**
* Google App Engine Admin API
*
* The App Engine Admin API enables developers to provision and manage their App Engine applications.
*
* @example
* var google = require('googleapis');
* var appengine = google.appengine('v1alpha');
*
* @namespace appengine
* @type {Function}
* @version v1alpha
* @variation v1alpha
* @param {object=} options Options for Appengine
*/
...
appsactivity = function () { [native code] }
n/a
appstate = function () { [native code] }
n/a
function Compute() { Compute.super_.call(this); // Start with an expired refresh token, which will automatically be refreshed // before the first API call is made. this.credentials = { refresh_token: 'compute-placeholder', expiry_date: 1 }; // Hook the post request method so we can provide better error messages. this._postRequest = this._injectErrorMessage; }
n/a
function JWT(email, keyFile, key, scopes, subject) { JWT.super_.call(this); this.email = email; this.keyFile = keyFile; this.key = key; this.scopes = scopes; this.subject = subject; this.gToken = gToken; this.credentials = { refresh_token: 'jwt-placeholder', expiry_date: 1 }; }
n/a
function AuthClient() { this.transporter = new DefaultTransporter(); }
n/a
bigquery = function () { [native code] }
...
You can specify an `auth` object to be used per request. Each request also
inherits the options specified at the service level and global level.
For example:
```js
var google = require('googleapis');
var bigquery = google.bigquery('v2');
// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
// environment variables.
google.auth.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
console.log('Authentication failed because of ', err);
return;
...
blogger = function () { [native code] }
n/a
books = function () { [native code] }
n/a
calendar = function () { [native code] }
n/a
civicinfo = function () { [native code] }
n/a
classroom = function () { [native code] }
n/a
cloudbilling = function () { [native code] }
n/a
cloudbuild = function () { [native code] }
n/a
clouddebugger = function () { [native code] }
n/a
clouderrorreporting = function () { [native code] }
...
* Stackdriver Error Reporting API
*
* Stackdriver Error Reporting groups and counts similar errors from cloud services. The Stackdriver Error Reporting API provides
a way to report new errors and read access to error groups and their associated errors.
*
* @example
* var google = require('googleapis');
* var clouderrorreporting = google.clouderrorreporting('v1beta1');
*
* @namespace clouderrorreporting
* @type {Function}
* @version v1beta1
* @variation v1beta1
* @param {object=} options Options for Clouderrorreporting
*/
...
cloudfunctions = function () { [native code] }
...
/**
* Google Cloud Functions API
*
* API for managing lightweight user-provided functions executed in response to events.
*
* @example
* var google = require('googleapis');
* var cloudfunctions = google.cloudfunctions('v1beta2');
*
* @namespace cloudfunctions
* @type {Function}
* @version v1beta2
* @variation v1beta2
* @param {object=} options Options for Cloudfunctions
*/
...
cloudkms = function () { [native code] }
n/a
cloudmonitoring = function () { [native code] }
...
/**
* Cloud Monitoring API
*
* Accesses Google Cloud Monitoring data.
*
* @example
* var google = require('googleapis');
* var cloudmonitoring = google.cloudmonitoring('v2beta2');
*
* @namespace cloudmonitoring
* @type {Function}
* @version v2beta2
* @variation v2beta2
* @param {object=} options Options for Cloudmonitoring
*/
...
cloudresourcemanager = function () { [native code] }
n/a
cloudtrace = function () { [native code] }
n/a
clouduseraccounts = function () { [native code] }
...
/**
* Cloud User Accounts API
*
* Creates and manages users and groups for accessing Google Compute Engine virtual machines.
*
* @example
* var google = require('googleapis');
* var clouduseraccounts = google.clouduseraccounts('alpha');
*
* @namespace clouduseraccounts
* @type {Function}
* @version alpha
* @variation alpha
* @param {object=} options Options for Clouduseraccounts
*/
...
compute = function () { [native code] }
...
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/compute'
]);
}
// Fetch the list of GCE zones within a project.
// NOTE: You must fill in your valid project ID before running this sample!
var compute = google.compute({
version: 'v1',
auth: authClient
});
var projectId = 'YOUR_PROJECT_ID';
compute.zones.list({
project: projectId,
...
consumersurveys = function () { [native code] }
n/a
container = function () { [native code] }
n/a
content = function () { [native code] }
...
/**
* Content API for Shopping
*
* Manages product items, inventory, and Merchant Center accounts for Google Shopping.
*
* @example
* var google = require('googleapis');
* var content = google.content('v2sandbox');
*
* @namespace content
* @type {Function}
* @version v2sandbox
* @variation v2sandbox
* @param {object=} options Options for Content
*/
...
customsearch = function () { [native code] }
n/a
dataflow = function () { [native code] }
...
/**
* Google Dataflow API
*
* Manages Google Cloud Dataflow projects on Google Cloud Platform.
*
* @example
* var google = require('googleapis');
* var dataflow = google.dataflow('v1b3');
*
* @namespace dataflow
* @type {Function}
* @version v1b3
* @variation v1b3
* @param {object=} options Options for Dataflow
*/
...
dataproc = function () { [native code] }
...
/**
* Google Cloud Dataproc API
*
* Manages Hadoop-based clusters and jobs on Google Cloud Platform.
*
* @example
* var google = require('googleapis');
* var dataproc = google.dataproc('v1alpha1');
*
* @namespace dataproc
* @type {Function}
* @version v1alpha1
* @variation v1alpha1
* @param {object=} options Options for Dataproc
*/
...
datastore = function () { [native code] }
...
* Google Cloud Datastore API
*
* Accesses the schemaless NoSQL database to provide fully managed, robust, scalable storage for your application.
*
* @example
* var google = require('googleapis');
* var datastore = google.datastore('v1beta3');
*
* @namespace datastore
* @type {Function}
* @version v1beta3
* @variation v1beta3
* @param {object=} options Options for Datastore
*/
...
deploymentmanager = function () { [native code] }
...
/**
* Google Cloud Deployment Manager API V2Beta Methods
*
* The Deployment Manager API allows users to declaratively configure, deploy and run complex solutions on the Google Cloud Platform
.
*
* @example
* var google = require('googleapis');
* var deploymentmanager = google.deploymentmanager('v2beta');
*
* @namespace deploymentmanager
* @type {Function}
* @version v2beta
* @variation v2beta
* @param {object=} options Options for Deploymentmanager
*/
...
dfareporting = function () { [native code] }
...
/**
* DCM/DFA Reporting And Trafficking API
*
* Manages your DoubleClick Campaign Manager ad campaigns and reports.
*
* @example
* var google = require('googleapis');
* var dfareporting = google.dfareporting('v2.6');
*
* @namespace dfareporting
* @type {Function}
* @version v2.6
* @variation v2.6
* @param {object=} options Options for Dfareporting
*/
...
discovery = function () { [native code] }
n/a
dlp = function () { [native code] }
n/a
dns = function () { [native code] }
n/a
doubleclickbidmanager = function () { [native code] }
n/a
doubleclicksearch = function () { [native code] }
n/a
drive = function () { [native code] }
...
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
var drive = google.drive({
version: 'v2',
auth: oauth2Client
});
```
See the [Options section][options] for more information.
...
firebasedynamiclinks = function () { [native code] }
n/a
firebaserules = function () { [native code] }
n/a
fitness = function () { [native code] }
n/a
fusiontables = function () { [native code] }
n/a
games = function () { [native code] }
n/a
gamesConfiguration = function () { [native code] }
...
/**
* Google Play Game Services Publishing API
*
* The Publishing API for Google Play Game Services.
*
* @example
* var google = require('googleapis');
* var gamesConfiguration = google.gamesConfiguration('v1configuration');
*
* @namespace gamesConfiguration
* @type {Function}
* @version v1configuration
* @variation v1configuration
* @param {object=} options Options for Gamesconfiguration
*/
...
gamesManagement = function () { [native code] }
...
/**
* Google Play Game Services Management API
*
* The Management API for Google Play Game Services.
*
* @example
* var google = require('googleapis');
* var gamesManagement = google.gamesManagement('v1management');
*
* @namespace gamesManagement
* @type {Function}
* @version v1management
* @variation v1management
* @param {object=} options Options for Gamesmanagement
*/
...
genomics = function () { [native code] }
...
/**
* Genomics API
*
* Upload, process, query, and search Genomics data in the cloud.
*
* @example
* var google = require('googleapis');
* var genomics = google.genomics('v1alpha2');
*
* @namespace genomics
* @type {Function}
* @version v1alpha2
* @variation v1alpha2
* @param {object=} options Options for Genomics
*/
...
gmail = function () { [native code] }
n/a
groupsmigration = function () { [native code] }
n/a
groupssettings = function () { [native code] }
n/a
iam = function () { [native code] }
n/a
identitytoolkit = function () { [native code] }
n/a
kgsearch = function () { [native code] }
n/a
language = function () { [native code] }
n/a
licensing = function () { [native code] }
n/a
logging = function () { [native code] }
n/a
function LoginTicket(env, pay) { var envelope = env; var payload = pay; this.getEnvelope = function() { return envelope; }; this.getPayload = function() { return payload; }; }
n/a
manufacturers = function () { [native code] }
n/a
mirror = function () { [native code] }
n/a
ml = function () { [native code] }
n/a
monitoring = function () { [native code] }
n/a
oauth2 = function () { [native code] }
n/a
function OAuth2Client(clientId, clientSecret, redirectUri, opt_opts) { OAuth2Client.super_.call(this); this.clientId_ = clientId; this.clientSecret_ = clientSecret; this.redirectUri_ = redirectUri; this.opts = opt_opts || {}; this.credentials = {}; }
n/a
pagespeedonline = function () { [native code] }
n/a
partners = function () { [native code] }
n/a
people = function () { [native code] }
n/a
playmoviespartner = function () { [native code] }
n/a
plus = function () { [native code] }
...
we cannot reliably restart your media stream). Set `expiry_date` to `true` to
force a refresh.
The following sample retrieves Google+ profile of the authenticated user.
``` js
var google = require('googleapis');
var plus = google.plus('v1');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
...
plusDomains = function () { [native code] }
n/a
prediction = function () { [native code] }
...
/**
* Prediction API
*
* Lets you access a cloud hosted machine learning service that makes it easy to build smart apps
*
* @example
* var google = require('googleapis');
* var prediction = google.prediction('v1.5');
*
* @namespace prediction
* @type {Function}
* @version v1.5
* @variation v1.5
* @param {object=} options Options for Prediction
*/
...
proximitybeacon = function () { [native code] }
n/a
pubsub = function () { [native code] }
...
* Google Cloud Pub/Sub API
*
* Provides reliable, many-to-many, asynchronous messaging between applications.
*
* @example
* var google = require('googleapis');
* var pubsub = google.pubsub('v1beta1a');
*
* @namespace pubsub
* @type {Function}
* @version v1beta1a
* @variation v1beta1a
* @param {object=} options Options for Pubsub
*/
...
qpxExpress = function () { [native code] }
n/a
replicapool = function () { [native code] }
n/a
replicapoolupdater = function () { [native code] }
n/a
reseller = function () { [native code] }
n/a
resourceviews = function () { [native code] }
n/a
runtimeconfig = function () { [native code] }
n/a
safebrowsing = function () { [native code] }
n/a
script = function () { [native code] }
n/a
searchconsole = function () { [native code] }
n/a
servicecontrol = function () { [native code] }
n/a
servicemanagement = function () { [native code] }
n/a
serviceuser = function () { [native code] }
n/a
sheets = function () { [native code] }
n/a
siteVerification = function () { [native code] }
n/a
slides = function () { [native code] }
n/a
sourcerepo = function () { [native code] }
n/a
spanner = function () { [native code] }
n/a
spectrum = function () { [native code] }
...
/**
* Google Spectrum Database API
*
* API for spectrum-management functions.
*
* @example
* var google = require('googleapis');
* var spectrum = google.spectrum('v1explorer');
*
* @namespace spectrum
* @type {Function}
* @version v1explorer
* @variation v1explorer
* @param {object=} options Options for Spectrum
*/
...
speech = function () { [native code] }
n/a
sqladmin = function () { [native code] }
n/a
storage = function () { [native code] }
n/a
storagetransfer = function () { [native code] }
n/a
surveys = function () { [native code] }
n/a
tagmanager = function () { [native code] }
n/a
taskqueue = function () { [native code] }
n/a
tasks = function () { [native code] }
n/a
toolresults = function () { [native code] }
...
/**
* Cloud Tool Results firstparty API
*
* Reads and publishes results from Firebase Test Lab.
*
* @example
* var google = require('googleapis');
* var toolresults = google.toolresults('v1beta3firstparty');
*
* @namespace toolresults
* @type {Function}
* @version v1beta3firstparty
* @variation v1beta3firstparty
* @param {object=} options Options for Toolresults
*/
...
tracing = function () { [native code] }
n/a
translate = function () { [native code] }
n/a
function DefaultTransporter() {}
n/a
urlshortener = function () { [native code] }
...
## Usage
Example: Creates a URL Shortener client and retrieves the long url of the
given short url:
``` js
var google = require('googleapis');
var urlshortener = google.urlshortener('v1');
var params = {
shortUrl: 'http://goo.gl/xKbRu3'
};
// get the long url of a shortened url
urlshortener.url.get(params, function (err, response) {
...
vision = function () { [native code] }
n/a
webfonts = function () { [native code] }
n/a
webmasters = function () { [native code] }
n/a
youtube = function () { [native code] }
n/a
youtubeAnalytics = function () { [native code] }
n/a
youtubereporting = function () { [native code] }
n/a
function GoogleApis(options) {
this.options(options);
this.addAPIs(apis);
/**
* A reference to an instance of GoogleAuth.
*
* @name GoogleApis#auth
* @type {GoogleAuth}
*/
this.auth = new GoogleAuth();
/**
* A reference to the {@link GoogleApis} constructor function.
*
* @name GoogleApis#GoogleApis
* @see GoogleApis
* @type {Function}
*/
this.GoogleApis = GoogleApis;
}
n/a
addAPIs = function (apis) { for (var apiName in apis) { this[apiName] = apis[apiName].bind(this); } }
n/a
discover = function (url, callback) { var self = this; discovery.discoverAllAPIs(url, function (err, apis) { if (err) { return callback(err); } self.addAPIs(apis); callback(); }); }
n/a
discoverAPI = function (path, options, callback) { var self = this; if (typeof options === 'function') { callback = options; options = {}; } if (!options) { options = {}; } discovery.discoverAPI(path, function (err, Endpoint) { if (err) { return callback(err); } var ep = new Endpoint(options); ep.google = self; // for drive.google.transporter return callback(null, Object.freeze(ep)); // create new & freeze }); }
...
}, function (err, resp) {
if (err) {
return handleError(err, callback);
}
async.parallel(resp.items.map(function (api) {
return function (cb) {
self.discoverAPI(api.discoveryRestUrl, function (err, _api) {
if (err) {
return cb(err);
}
api.api = _api;
cb(null, api);
});
};
...
options = function (options) { this._options = options || {}; }
...
var oauth2Client = new OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
// set auth as a global default
google.options({
auth: oauth2Client
});
```
Example: Setting a service-level `auth` option.
``` js
...
function Compute() { Compute.super_.call(this); // Start with an expired refresh token, which will automatically be refreshed // before the first API call is made. this.credentials = { refresh_token: 'compute-placeholder', expiry_date: 1 }; // Hook the post request method so we can provide better error messages. this._postRequest = this._injectErrorMessage; }
n/a
function JWT(email, keyFile, key, scopes, subject) { JWT.super_.call(this); this.email = email; this.keyFile = keyFile; this.key = key; this.scopes = scopes; this.subject = subject; this.gToken = gToken; this.credentials = { refresh_token: 'jwt-placeholder', expiry_date: 1 }; }
n/a
function Compute() { Compute.super_.call(this); // Start with an expired refresh token, which will automatically be refreshed // before the first API call is made. this.credentials = { refresh_token: 'compute-placeholder', expiry_date: 1 }; // Hook the post request method so we can provide better error messages. this._postRequest = this._injectErrorMessage; }
n/a
function OAuth2Client(clientId, clientSecret, redirectUri, opt_opts) { OAuth2Client.super_.call(this); this.clientId_ = clientId; this.clientSecret_ = clientSecret; this.redirectUri_ = redirectUri; this.opts = opt_opts || {}; this.credentials = {}; }
n/a
_injectErrorMessage = function (err, result, response, callback) { if (response && response.statusCode) { var helpfulMessage = null; if (response.statusCode === 403) { helpfulMessage = 'A Forbidden error was returned while attempting to retrieve an access ' + 'token for the Compute Engine built-in service account. This may be because the Compute ' + 'Engine instance does not have the correct permission scopes specified.'; } else if (response.statusCode === 404) { helpfulMessage = 'A Not Found error was returned while attempting to retrieve an access' + 'token for the Compute Engine built-in service account. This may be because the Compute ' + 'Engine instance does not have any permission scopes specified.'; } if (helpfulMessage) { if (err && err.message) { helpfulMessage += ' ' + err.message; } if (err) { err.message = helpfulMessage; } else { err = new Error(helpfulMessage); err.code = response.statusCode; } } } callback(err, result, response); }
n/a
createScopedRequired = function () { // On compute engine, scopes are specified at the compute instance's creation time, // and cannot be changed. For this reason, always return false. return false; }
...
throw err;
}
// The createScopedRequired method returns true when running on GAE or a local developer
// machine. In that case, the desired scopes must be passed in manually. When the code is
// running in GCE or a Managed VM, the scopes are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single, space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/compute'
]);
}
// Fetch the list of GCE zones within a project.
...
refreshToken_ = function (ignored_, opt_callback) { var uri = this.opts.tokenUrl || Compute.GOOGLE_OAUTH2_TOKEN_URL_; // request for new token this.transporter.request({ method: 'GET', uri: uri, json: true }, function(err, tokens, response) { if (!err && tokens && tokens.expires_in) { tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000)); delete tokens.expires_in; } if (opt_callback) { opt_callback(err, tokens, response); } }); }
n/a
_makeRequest = function (opts, callback) { return this.transporter.request(opts, callback); }
n/a
_postRequest = function (err, result, response, callback) { callback(err, result, response); }
n/a
decodeBase64 = function (b64String) { var buffer = new Buffer(b64String, 'base64'); return buffer.toString('utf8'); }
n/a
generateAuthUrl = function (opt_opts) { var opts = opt_opts || {}; opts.response_type = opts.response_type || 'code'; opts.client_id = opts.client_id || this.clientId_; opts.redirect_uri = opts.redirect_uri || this.redirectUri_; // Allow scopes to be passed either as array or a string if (opts.scope instanceof Array) { opts.scope = opts.scope.join(' '); } var rootUrl = this.opts.authBaseUrl || OAuth2Client.GOOGLE_OAUTH2_AUTH_BASE_URL_; return rootUrl + '?' + querystring.stringify(opts); }
...
// generate a url that asks permissions for Google+ and Google Calendar scopes
var scopes = [
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/calendar'
];
var url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: scopes,
// Optional property that passes state parameters to redirect URI
...
getAccessToken = function (callback) { var credentials = this.credentials; var expiryDate = credentials.expiry_date; // if no expiry time, assume it's not expired var isTokenExpired = expiryDate ? expiryDate <= (new Date()).getTime() : false; if (!credentials.access_token && !credentials.refresh_token) { return callback(new Error('No access or refresh token is set.'), null); } var shouldRefresh = !credentials.access_token || isTokenExpired; if (shouldRefresh && credentials.refresh_token) { if (!this.credentials.refresh_token) { return callback(new Error('No refresh token is set.'), null); } this.refreshAccessToken(function(err, tokens, response) { if (err) { return callback(err, null, response); } if (!tokens || (tokens && !tokens.access_token)) { return callback(new Error('Could not refresh access token.'), null, response); } return callback(null, tokens.access_token, response); }); } else { return callback(null, credentials.access_token, null); } }
n/a
getFederatedSignonCerts = function (callback) { var nowTime = (new Date()).getTime(); if (certificateExpiry && (nowTime < certificateExpiry.getTime())) { callback(null, certificateCache); return; } this.transporter.request({ method: 'GET', uri: OAuth2Client.GOOGLE_OAUTH2_FEDERATED_SIGNON_CERTS_URL_, json: true }, function(err, body, response) { if (err) { callback('Failed to retrieve verification certificates: ' + err, null, response); return; } var cacheControl = response.headers['cache-control']; var cacheAge = -1; if (cacheControl) { var pattern = new RegExp('max-age=([0-9]*)'); var regexResult = pattern.exec(cacheControl); if (regexResult.length === 2) { // Cache results with max-age (in seconds) cacheAge = regexResult[1] * 1000; // milliseconds } } var now = new Date(); certificateExpiry = cacheAge === -1 ? null : new Date(now.getTime() + cacheAge); certificateCache = body; callback(null, body, response); }); }
n/a
getRequestMetadata = function (opt_uri, metadataCb) { var that = this; var thisCreds = this.credentials; if (!thisCreds.access_token && !thisCreds.refresh_token) { return metadataCb(new Error('No access or refresh token is set.'), null); } // if no expiry time, assume it's not expired var expiryDate = thisCreds.expiry_date; var isTokenExpired = expiryDate ? expiryDate <= (new Date()).getTime() : false; if (thisCreds.access_token && !isTokenExpired) { thisCreds.token_type = thisCreds.token_type || 'Bearer'; var headers = {'Authorization': thisCreds.token_type + ' ' + thisCreds.access_token }; return metadataCb(null, headers , null); } return this.refreshToken_(thisCreds.refresh_token, function(err, tokens, response) { if (err) { return metadataCb(err, null, response); } else { if (!tokens || (tokens && !tokens.access_token)) { return metadataCb(new Error('Could not refresh access token.'), null, response); } var credentials = that.credentials; credentials.token_type = credentials.token_type || 'Bearer'; tokens.refresh_token = credentials.refresh_token; that.credentials = tokens; var headers = {'Authorization': credentials.token_type + ' ' + tokens.access_token }; return metadataCb(err, headers , response); } }); }
n/a
getToken = function (code, opt_callback) { var uri = this.opts.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_; var values = { code: code, client_id: this.clientId_, client_secret: this.clientSecret_, redirect_uri: this.redirectUri_, grant_type: 'authorization_code' }; this.transporter.request({ method: 'POST', uri: uri, form: values, json: true }, function(err, tokens, response) { if (!err && tokens && tokens.expires_in) { tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000)); delete tokens.expires_in; } var done = opt_callback || noop; done(err, tokens, response); }); }
...
GET /oauthcallback?code={authorizationCode}
##### Retrieve access token
With the code returned, you can ask for an access token as shown below:
``` js
oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth2Client.setCredentials(tokens);
}
});
```
...
refreshAccessToken = function (callback) { var that = this; if (!this.credentials.refresh_token) { callback(new Error('No refresh token is set.'), null); return; } this.refreshToken_(this.credentials.refresh_token, function(err, result, response) { if (err) { callback(err, null, response); } else { var tokens = result; tokens.refresh_token = that.credentials.refresh_token; that.credentials = tokens; callback(null, that.credentials, response); } }); }
...
##### Manually refreshing access token
If you need to manually refresh the `access_token` associated with your OAuth2
client, make sure you have a `refresh_token` set in your credentials first and
then call:
``` js
oauth2Client.refreshAccessToken(function(err, tokens) {
// your access_token is now refreshed and stored in oauth2Client
// store these new tokens in a safe place (e.g. database)
});
```
#### Using API keys
...
refreshToken_ = function (refresh_token, opt_callback) { var uri = this.opts.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_; var values = { refresh_token: refresh_token, client_id: this.clientId_, client_secret: this.clientSecret_, grant_type: 'refresh_token' }; // request for new token return this.transporter.request({ method: 'POST', uri: uri, form: values, json: true }, function(err, tokens, response) { if (!err && tokens && tokens.expires_in) { tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000)); delete tokens.expires_in; } var done = opt_callback || noop; done(err, tokens, response); }); }
n/a
request = function (opts, callback) {
/* jshint latedef:false */
var that = this;
// Callbacks will close over this to ensure that we only retry once
var retry = true;
// Hook the callback routine to call the _postRequest method.
var postRequestCb = function(err, body, resp) {
var statusCode = resp && resp.statusCode;
// Automatically retry 401 and 403 responses
// if err is set and is unrelated to response
// then getting credentials failed, and retrying won't help
if (retry && (statusCode === 401 || statusCode === 403) &&
(!err || err.code === statusCode)) {
/* It only makes sense to retry once, because the retry is intended to
* handle expiration-related failures. If refreshing the token does not
* fix the failure, then refreshing again probably won't help */
retry = false;
// Force token refresh
that.refreshAccessToken(function() {
that.getRequestMetadata(unusedUri, authCb);
});
} else {
that._postRequest(err, body, resp, callback);
}
};
var authCb = function(err, headers, response) {
if (err) {
postRequestCb(err, null, response);
} else {
if (headers) {
opts.headers = opts.headers || {};
opts.headers.Authorization = headers.Authorization;
}
return that._makeRequest(opts, postRequestCb);
}
};
var unusedUri = null;
return this.getRequestMetadata(unusedUri, authCb);
}
...
options
);
delete options.auth; // is overridden by our auth code
delete options.params; // We handle params ourselves and Request does not recognise 'params'
// create request (using authClient or otherwise and return request obj)
if (authClient) {
req = authClient.request(options, callback);
} else {
req = new DefaultTransporter().request(options, callback);
}
if (body) {
body.pipe(req);
}
...
revokeCredentials = function (callback) { var token = this.credentials.access_token; this.credentials = {}; if (token) { this.revokeToken(token, callback); } else { callback(new Error('No access token to revoke.'), null); } }
n/a
revokeToken = function (token, opt_callback) { this.transporter.request({ uri: OAuth2Client.GOOGLE_OAUTH2_REVOKE_URL_ + '?' + querystring.stringify({ token: token }), json: true }, opt_callback); }
n/a
verifyIdToken = function (idToken, audience, callback) { if (!idToken || !callback) { throw new Error('The verifyIdToken method requires both ' + 'an ID Token and a callback method'); } this.getFederatedSignonCerts(function(err, certs) { if (err) { callback(err, null); } var login; try { login = this.verifySignedJwtWithCerts(idToken, certs, audience, OAuth2Client.ISSUERS_); } catch (err) { callback(err); return; } callback(null, login); }.bind(this)); }
n/a
verifySignedJwtWithCerts = function (jwt, certs, requiredAudience, issuers, maxExpiry) { if (!maxExpiry) { maxExpiry = OAuth2Client.MAX_TOKEN_LIFETIME_SECS_; } var segments = jwt.split('.'); if (segments.length !== 3) { throw new Error('Wrong number of segments in token: ' + jwt); } var signed = segments[0] + '.' + segments[1]; var signature = segments[2]; var envelope, payload; try { envelope = JSON.parse(this.decodeBase64(segments[0])); } catch (err) { } if (!envelope) { throw new Error('Can\'t parse token envelope: ' + segments[0]); } try { payload = JSON.parse(this.decodeBase64(segments[1])); } catch (err) { } if (!payload) { throw new Error('Can\'t parse token payload: ' + segments[1]); } if (!certs.hasOwnProperty(envelope.kid)) { // If this is not present, then there's no reason to attempt verification throw new Error('No pem found for envelope: ' + JSON.stringify(envelope)); } var pem = certs[envelope.kid]; var pemVerifier = new PemVerifier(); var verified = pemVerifier.verify(pem, signed, signature, 'base64'); if (!verified) { throw new Error('Invalid token signature: ' + jwt); } if (!payload.iat) { throw new Error('No issue time in token: ' + JSON.stringify(payload)); } if (!payload.exp) { throw new Error('No expiration time in token: ' + JSON.stringify(payload)); } var iat = parseInt(payload.iat, 10); var exp = parseInt(payload.exp, 10); var now = new Date().getTime() / 1000; if (exp >= now + maxExpiry) { throw new Error('Expiration time too far in future: ' + JSON.stringify(payload)); } var earliest = iat - OAuth2Client.CLOCK_SKEW_SECS_; var latest = exp + OAuth2Client.CLOCK_SKEW_SECS_; if (now < earliest) { throw new Error('Token used too early, ' + now + ' < ' + earliest + ': ' + JSON.stringify(payload)); } if (now > latest) { throw new Error('Token used too late, ' + now + ' > ' + latest + ': ' + JSON.stringify(payload)); } if (issuers && issuers.indexOf(payload.iss) < 0) { throw new Error('Invalid issuer, expected one of [' + issuers + '], but got ' + payload.iss); } // Check the audience matches if we have one if (typeof requiredAudience !== 'undefined' && requiredAudience !== null) { var aud = payload.aud; var audVerified = false; //If the requiredAudience is an array, check if it contains token audience if(requiredAudience.constructor === Array) { audVerified = (requiredAudience.indexOf(aud) > -1); } else{ audVerified = (aud === requiredAudience); } if (!audVerified) { throw new Error('Wrong recipient, payload audience != requiredAudience'); } } return new LoginTicket(envelope, payload); }
n/a
function JWT(email, keyFile, key, scopes, subject) { JWT.super_.call(this); this.email = email; this.keyFile = keyFile; this.key = key; this.scopes = scopes; this.subject = subject; this.gToken = gToken; this.credentials = { refresh_token: 'jwt-placeholder', expiry_date: 1 }; }
n/a
function OAuth2Client(clientId, clientSecret, redirectUri, opt_opts) { OAuth2Client.super_.call(this); this.clientId_ = clientId; this.clientSecret_ = clientSecret; this.redirectUri_ = redirectUri; this.opts = opt_opts || {}; this.credentials = {}; }
n/a
_createGToken = function (callback) { if (this.gtoken) { return callback(null, this.gtoken); } else { this.gtoken = this.gToken({ iss: this.email, sub: this.subject, scope: this.scopes, keyFile: this.keyFile, key: this.key }); return callback(null, this.gtoken); } }
n/a
authorize = function (opt_callback) { var that = this; var done = opt_callback || noop; that.refreshToken_(null, function(err, result) { if (!err) { that.credentials = result; that.credentials.refresh_token = 'jwt-placeholder'; that.key = that.gtoken.key; that.email = that.gtoken.iss; } done(err, result); }); }
...
key.client_email,
null,
key.private_key,
[scope1, scope2],
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
// Make an authorized request to list Drive files.
drive.files.list({
...
createScoped = function (scopes) { return new JWT(this.email, this.keyFile, this.key, scopes, this.subject); }
...
// The createScopedRequired method returns true when running on GAE or a local developer
// machine. In that case, the desired scopes must be passed in manually. When the code is
// running in GCE or a Managed VM, the scopes are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single, space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/compute'
]);
}
// Fetch the list of GCE zones within a project.
// NOTE: You must fill in your valid project ID before running this sample!
var compute = google.compute({
...
createScopedRequired = function () { // If scopes is null, always return true. if (this.scopes) { // For arrays, check the array length. if (this.scopes instanceof Array) { return this.scopes.length === 0; } // For others, convert to a string and check the length. return String(this.scopes).length === 0; } return true; }
...
throw err;
}
// The createScopedRequired method returns true when running on GAE or a local developer
// machine. In that case, the desired scopes must be passed in manually. When the code is
// running in GCE or a Managed VM, the scopes are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single, space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/compute'
]);
}
// Fetch the list of GCE zones within a project.
...
fromJSON = function (json, opt_callback) { var that = this; var done = opt_callback || noop; if (!json) { done(new Error( 'Must pass in a JSON object containing the service account auth settings.')); return; } if (!json.client_email) { done(new Error( 'The incoming JSON object does not contain a client_email field')); return; } if (!json.private_key) { done(new Error( 'The incoming JSON object does not contain a private_key field')); return; } // Extract the relevant information from the json key file. that.email = json.client_email; that.key = json.private_key; that.projectId = json.project_id; done(); }
n/a
fromStream = function (stream, opt_callback) { var that = this; var done = opt_callback || noop; if (!stream) { process.nextTick(function() { done( new Error('Must pass in a stream containing the service account auth settings.')); }); return; } var s = ''; stream.setEncoding('utf8'); stream.on('data', function (chunk) { s += chunk; }); stream.on('end', function () { try { var data = JSON.parse(s); that.fromJSON(data, opt_callback); } catch (err) { done(err); } }); }
n/a
getRequestMetadata = function (opt_uri, metadataCb) { if (this.createScopedRequired() && opt_uri) { // no scopes have been set, but a uri has been provided. Use JWTAccess credentials. var alt = new JWTAccess(this.email, this.key); return alt.getRequestMetadata(opt_uri, metadataCb); } else { return JWT.super_.prototype.getRequestMetadata.call( this, opt_uri, metadataCb); } }
n/a
refreshToken_ = function (ignored_, opt_callback) { var done = opt_callback || noop; return this._createGToken(function(err, gToken) { if (err) { return done(err); } else { return gToken.getToken(function (err, token) { return done(err, { access_token: token, token_type: 'Bearer', expiry_date: gToken.expires_at }); }); } }); }
n/a
function AuthClient() { this.transporter = new DefaultTransporter(); }
n/a
request = function () { throw new Error('Not implemented yet.'); }
...
options
);
delete options.auth; // is overridden by our auth code
delete options.params; // We handle params ourselves and Request does not recognise 'params'
// create request (using authClient or otherwise and return request obj)
if (authClient) {
req = authClient.request(options, callback);
} else {
req = new DefaultTransporter().request(options, callback);
}
if (body) {
body.pipe(req);
}
...
setCredentials = function (credentials) { this.credentials = credentials; }
...
With the code returned, you can ask for an access token as shown below:
``` js
oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth2Client.setCredentials(tokens);
}
});
```
##### Setting global or service-level auth
You can set the `auth` as a global or service-level option so you don't need to
...
function Discovery(options) { this.options = options || {}; }
n/a
discoverAPI = function (apiDiscoveryUrl, callback) { function _generate (err, resp) { if (err) { return handleError(err, callback); } return callback(null, makeEndpoint(resp)); } if (typeof apiDiscoveryUrl === 'string') { var parts = url.parse(apiDiscoveryUrl); if (apiDiscoveryUrl && !parts.protocol) { this.log('Reading from file ' + apiDiscoveryUrl); try { return fs.readFile(apiDiscoveryUrl, { encoding: 'utf8' }, function (err, file) { _generate(err, JSON.parse(file)); }); } catch (err) { return handleError(err, callback); } } else { this.log('Requesting ' + apiDiscoveryUrl); transporter.request({ uri: apiDiscoveryUrl }, _generate); } } else { var options = apiDiscoveryUrl; this.log('Requesting ' + options.url); var parameters = { options: { url: options.url, method: 'GET' }, requiredParams: [], pathParams: [], context: { google: { _options: {} }, _options: {} } }; delete options.url; parameters.params = options; createAPIRequest(parameters, _generate); } }
...
}, function (err, resp) {
if (err) {
return handleError(err, callback);
}
async.parallel(resp.items.map(function (api) {
return function (cb) {
self.discoverAPI(api.discoveryRestUrl, function (err, _api) {
if (err) {
return cb(err);
}
api.api = _api;
cb(null, api);
});
};
...
discoverAllAPIs = function (discoveryUrl, callback) { var self = this; var headers = this.options.includePrivate ? {} : { 'X-User-Ip': '0.0.0.0' }; transporter.request({ uri: discoveryUrl, headers: headers }, function (err, resp) { if (err) { return handleError(err, callback); } async.parallel(resp.items.map(function (api) { return function (cb) { self.discoverAPI(api.discoveryRestUrl, function (err, _api) { if (err) { return cb(err); } api.api = _api; cb(null, api); }); }; }), function (err, apis) { if (err) { return callback(err); } var versionIndex = {}; var apisIndex = {}; apis.forEach(function (api) { if (!apisIndex[api.name]) { versionIndex[api.name] = {}; apisIndex[api.name] = function (options) { var type = typeof options; var version; if (type === 'string') { version = options; options = {}; } else if (type === 'object') { version = options.version; delete options.version; } else { throw new Error('Argument error: Accepts only string or object'); } try { var Endpoint = versionIndex[api.name][version]; var ep = new Endpoint(options); ep.google = this; // for drive.google.transporter return Object.freeze(ep); // create new & freeze } catch (e) { throw new Error(util.format('Unable to load endpoint %s("%s"): %s', api.name, version, e.message)); } }; } versionIndex[api.name][api.version] = api.api; }); return callback(null, apisIndex); }); }); }
n/a
log = function () { if (this.options && this.options.debug) { console.log.apply(this, arguments); } }
...
var params = {
shortUrl: 'http://goo.gl/xKbRu3'
};
// get the long url of a shortened url
urlshortener.url.get(params, function (err, response) {
if (err) {
console.log('Encountered error', err);
} else {
console.log('Long url is', response.longUrl);
}
});
```
### Create a service client
...
function DefaultTransporter() {}
n/a
function buildurl(input) { return ('\'' + input + '\'') // No * symbols .replace(/\*/g, '') // No + symbols .replace(/\+/g, '') // replace double slashes with single slash (except in https://) .replace(/([^:]\/)\/+/g, '$1') // No {/ symbols .replace(/\{\//g, '/{'); }
n/a
function handleError(err, callback) { if (callback && typeof callback === 'function') { callback(err, null); } }
n/a
function LoginTicket(env, pay) { var envelope = env; var payload = pay; this.getEnvelope = function() { return envelope; }; this.getPayload = function() { return payload; }; }
n/a
getAttributes = function () { return { 'envelope': this.getEnvelope(), 'payload': this.getPayload() }; }
n/a
getUserId = function () { var payload = this.getPayload(); if (payload && payload[USER_ATTR]) { return payload[USER_ATTR]; } return null; }
n/a
function OAuth2Client(clientId, clientSecret, redirectUri, opt_opts) { OAuth2Client.super_.call(this); this.clientId_ = clientId; this.clientSecret_ = clientSecret; this.redirectUri_ = redirectUri; this.opts = opt_opts || {}; this.credentials = {}; }
n/a
function AuthClient() { this.transporter = new DefaultTransporter(); }
n/a
function DefaultTransporter() {}
n/a
configure = function (opts) { // set transporter user agent opts.headers = opts.headers || {}; if (!opts.headers['User-Agent']) { opts.headers['User-Agent'] = this.USER_AGENT; } else if (opts.headers['User-Agent'].indexOf(this.USER_AGENT) === -1) { opts.headers['User-Agent'] = opts.headers['User-Agent'] + ' ' + this.USER_AGENT; } return opts; }
n/a
request = function (opts, opt_callback) { opts = this.configure(opts); return request(opts.uri || opts.url, opts, this.wrapCallback_(opt_callback)); }
...
options
);
delete options.auth; // is overridden by our auth code
delete options.params; // We handle params ourselves and Request does not recognise 'params'
// create request (using authClient or otherwise and return request obj)
if (authClient) {
req = authClient.request(options, callback);
} else {
req = new DefaultTransporter().request(options, callback);
}
if (body) {
body.pipe(req);
}
...
wrapCallback_ = function (opt_callback) { return function(err, res, body) { if (err || !body) { return opt_callback && opt_callback(err, body, res); } // Only and only application/json responses should // be decoded back to JSON, but there are cases API back-ends // responds without proper content-type. try { body = JSON.parse(body); } catch (err) { /* no op */ } if (body && body.error && res.statusCode !== 200) { if (typeof body.error === 'string') { err = new Error(body.error); err.code = res.statusCode; } else if (Array.isArray(body.error.errors)) { err = new Error(body.error.errors.map( function(err) { return err.message; } ).join('\n')); err.code = body.error.code; err.errors = body.error.errors; } else { err = new Error(body.error.message); err.code = body.error.code || res.statusCode; } body = null; } else if (res.statusCode >= 500) { // Consider all '500 responses' errors. err = new Error(body); err.code = res.statusCode; body = null; } if (opt_callback) { opt_callback(err, body, res); } }; }
n/a
extend = function (obj) { var source, prop; for (var i = 1, length = arguments.length; i < length; i++) { source = arguments[i]; for (prop in source) { if (source.hasOwnProperty(prop)) { obj[prop] = source[prop]; } } } return obj; }
...
* @param {object} parameters Parameters used to form request
* @param {Function} callback Callback when request finished or error found
* @return {Request} Returns Request object or null
*/
function createAPIRequest (parameters, callback) {
var req, body, missingParams;
var params = parameters.params;
var options = utils.extend({}, parameters.options);
// If the params are not present, and callback was passed instead,
// use params as the callback and create empty params.
if (typeof params === 'function') {
callback = params;
params = {};
}
...