GithubHelp home page GithubHelp logo

richierunner / google-api-nodejs-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googleapis/google-api-nodejs-client

0.0 2.0 0.0 42.46 MB

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included. API Reference Docs: http://google.github.io/google-api-nodejs-client/

License: Apache License 2.0

Python 0.01% Makefile 0.01% TypeScript 99.94% HTML 0.05%

google-api-nodejs-client's Introduction

Google Inc. logo

Google APIs Node.js Client

CircleCI Greenkeeper npm version Code Coverage Downloads Dependency Status devDependency Status Known Vulnerabilities

Node.js client library for using Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.

Library maintenance

This client library is supported but in maintenance mode only. We are fixing necessary bugs and adding essential features to ensure this library continues to meet your needs for accessing Google APIs. Non-critical issues will be closed. Any issue may be reopened if it is causing ongoing problems.

Table of Contents

Alpha

This library is in Alpha. We will make an effort to support the library, but we reserve the right to make incompatible changes when necessary.

Release Notes & Breaking Changes

You can find a detailed list of breaking changes and new features in our Release Notes. If you've used this library before 25.x, see our Release Notes to learn about migrating your code from 24.x.x to 25.x.x. It's pretty easy :)

Supported APIs

The full list of supported APIs can be found here. The API endpoints are automatically generated, so if the API is not in the list, it is currently not supported by this API client library.

Questions/problems?

Working with Google Cloud Platform APIs?

If you're working with Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, consider using the google-cloud package, an idiomatic Node.js client for Google Cloud Platform services.

You can find the list of Google Cloud Platform APIs supported by google-cloud in the google-cloud docs.

Installation

This library is distributed on npm. In order to add it as a dependency, run the following command:

$ npm install googleapis --save

Usage

Example Creates a URL Shortener client and retrieves the long url of the given short url:

const {google} = require('googleapis');
const urlshortener = google.urlshortener('v1');

const params = {
  shortUrl: 'http://goo.gl/xKbRu3',
  key: 'YOUR API KEY'
};

// get the long url of a shortened url
urlshortener.url.get(params, (err, response) => {
  if (err) {
    console.error(err);
    throw err;
  }
  console.log('Long url is', response.data.longUrl);
});

You can also use promises:

urlshortener.url.get(params)
  .then(response => {
    console.log('Long url is', response.data.longUrl);
  })
  .catch(error => console.error);

Or async/await:

async function runSample() {
  const response = await urlshortener.url.get(params);
  console.log('Long url is', response.data.longUrl);
}
runSample().catch(console.error);

Example Updates an email message's labels, using the resource parameter to specify the request body.

gmail.users.messages.modify({
  id: Number,
  resource: {
    addLabelIds: Array,
    removeLabelIds: Array
  },
  userId: 'me',
}, (err, response) => {
  console.log(response.data);
});

Create a service client

To interact with the various Google APIs you need to create a service client for that particular API. These are immutable objects you use to make API calls.

Example: Creating a urlshortener client with version v1 of the API.

const {google} = require('googleapis');
const urlshortener = google.urlshortener('v1');

Supported APIs are listed on the Google APIs Explorer.

Authorizing and authenticating

OAuth2 client

This client comes with an OAuth2 client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly if you also provide an expiry_date and the token is expired. The basics of Google's OAuth2 implementation is explained on Google Authorization and Authentication documentation.

In the following examples, you may need a CLIENT_ID, CLIENT_SECRET and REDIRECT_URL. You can find these pieces of information by going to the Developer Console, clicking your project --> APIs & auth --> credentials.

For more information about OAuth2 and how it works, see here.

A complete sample application that authorizes and authenticates with the OAuth2 client is available at samples/oauth2.js.

Generating an authentication URL

To ask for permissions from a user to retrieve an access token, you redirect them to a consent page. To create a consent page URL:

const {google} = require('googleapis');
const OAuth2 = google.auth.OAuth2;

const oauth2Client = new OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

// generate a url that asks permissions for Google+ and Google Calendar scopes
const scopes = [
  'https://www.googleapis.com/auth/plus.me',
  'https://www.googleapis.com/auth/calendar'
];

const 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
  // state: 'foo'
});
IMPORTANT NOTE

refresh_token is only returned on the first authorization. More details here

Retrieve authorization code

Once a user has given permissions on the consent page, Google will redirect the page to the redirect URL you have provided with a code query parameter.

GET /oauthcallback?code={authorizationCode}
Retrieve access token

With the code returned, you can ask for an access token as shown below:

oauth2Client.getToken(code, (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 specify it every request.

Example: Setting a global auth option.

const {google} = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const 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.

const {google} = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

const drive = google.drive({
  version: 'v2',
  auth: oauth2Client
});

See the Options section for more information.

Making authenticated requests

You can start using OAuth2 to authorize and authenticate your requests to Google APIs with the retrieved tokens. If you provide a refresh_token and expiry_date (milliseconds since the Unix Epoch) and the access_token has expired, the access_token will be automatically refreshed and the request is replayed (with the except of requests with a media body, as 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.

const {google} = require('googleapis');
const plus = google.plus('v1');
const OAuth2 = google.auth.OAuth2;

// WARNING: Make sure your CLIENT_SECRET is stored in a safe place.
const oauth2Client = new OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

// Retrieve tokens via token exchange explained above or set them:
oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
  // Optional, provide an expiry_date (milliseconds since the Unix Epoch)
  // expiry_date: (new Date()).getTime() + (1000 * 60 * 60 * 24 * 7)
});

plus.people.get({
  userId: 'me',
  auth: oauth2Client
}, function (err, response) {
  console.log(response.data);
});
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:

oauth2Client.refreshAccessToken((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

You may need to send an API key with the request you are going to make. The following uses an API key to make a request to the Google+ API service to retrieve a person's profile given a userId:

const {google} = require('googleapis');
const plus = google.plus('v1');

const API_KEY = 'ABC123'; // specify your API key here

plus.people.get({
  auth: API_KEY,
  userId: '+google'
}, (err, response) => {
  if (err) {
    console.error(err);
    throw err;
  }
  console.log(response.data.displayName);
});

Alternatively, you can specify the key parameter and it will get used:

plus.people.get({
  key: API_KEY,
  userId: '+google'
}, function (err, response) {
  if (err) {
    console.error(err);
    throw err;
  }
  console.log(response.data.displayName);
});

To learn more about API keys, please see the documentation.

Using JWT (Service Tokens)

The Google Developers Console provides .json file that you can use to configure a JWT auth client and authenticate your requests, for example when using a service account.

const {google} = require('googleapis');
const drive = google.drive('v3');

const key = require('/path/to/key.json');
const jwtClient = new google.auth.JWT({
  email: key.client_email,
  key: key.private_key,
  scopes: ['https://www.googleapis.com/auth/drive']
});

// Make an authorized request to list Drive files.
drive.files.list({
  auth: jwtClient
}, (err, response) => {
  // handle err and response
});

The parameters for the JWT auth client including how to use it with a .pem file are explained in samples/jwt.js.

Choosing the correct credential type automatically

Rather than manually creating an OAuth2 client, JWT client, or Compute client, the auth library can create the correct credential type for you, depending upon the environment your code is running under.

For example, a JWT auth client will be created when your code is running on your local developer machine, and a Compute client will be created when the same code is running on a configured instance of Google Compute Engine.

The code below shows how to retrieve a default credential type, depending upon the runtime environment. The createScopedRequired must be called to determine when you need to pass in the scopes manually, and when they have been set for you automatically based on the configured runtime environment.

async function main () {
  // This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS environment variables.
  const client = await google.auth.getClient();

  // Scopes can be specified either as an array or as a single, space-delimited string.
  if (client instanceof google.auth.JWT) {
    client.scopes = ['https://www.googleapis.com/auth/compute'];
  }

  // Fetch the list of GCE zones within a project.
  const project = await google.auth.getDefaultProjectId();
  const res = await compute.zones.list({ project, auth: client });
  console.log(res.data);
}

main().catch(console.error);

Specifying request body

The body of the request is specified in the resource parameter object of the request. The resource/body is specified as a JavaScript object with key/value pairs. See the example in the next section below for an example on how it is specified.

Media uploads

This client supports multipart media uploads. The resource parameters are specified in the resource parameter object, and the media itself is specified in the media.body parameter with mime-type specified in media.mimeType.

Example: Upload a plain text file to Google Drive

This example uploads a plain text file to Google Drive with the title "Test" and contents "Hello World".

const drive = google.drive({
  version: 'v3',
  auth: oauth2Client
});

drive.files.create({
  resource: {
    name: 'Test',
    mimeType: 'text/plain'
  },
  media: {
    mimeType: 'text/plain',
    body: 'Hello World'
  }
}, callback);

You can also upload media by specifying media.body as a Readable stream. This can allow you to upload very large files that cannot fit into memory.

Note: Your readable stream may be unstable. Use at your own risk.

Example: Upload an image to Google Drive from a readable stream
const fs = require('fs');
const drive = google.drive({
  version: 'v3',
  auth: oauth2Client
});

drive.files.create({
  resource: {
    name: 'testimage.png',
    mimeType: 'image/png'
  },
  media: {
    mimeType: 'image/png',
    body: fs.createReadStream('awesome.png') // read streams are awesome!
  }
}, callback);

For more examples of creation and modification requests with media attachments, take a look at the samples/drive/upload.js sample.

Options

For more fine-tuned control over how your API calls are made, we provide you with the ability to specify additional options that can be applied directly to the 'axios' object used in this library to make network calls to the API.

You may specify additional options either in the global google object or on a service client basis.

Available options

The options you specify are attached to the axios object so whatever axios supports, this library supports. You may also specify global or per-service request parameters that will be attached to all API calls you make.

A full list of supported options can be found here.

Global options

Example: Specifying a default timeout and auth to be used for each request

You can choose default options that will be sent with each request. For a list of available options, see the Axios Request Options.

const {google} = require('googleapis');
google.options({ timeout: 1000, auth: auth });

// All requests made with this object will use these settings unless overridden.
Example: Specifying global request parameters
const {google} = require('googleapis');
google.options({ params: { quotaUser: '[email protected]' } });

// All requests from all services will contain the above query parameter
// unless overridden either in a service client or in individual API calls.

Service-client options

You can also specify options when creating a service client.

Example: Specifying a default auth option (API key or OAuth2 client)
const auth = 'API KEY'; // or you could use oauth2Client
const urlshortener = google.urlshortener({ version: 'v1', auth: auth });

// All requests made with this object will use the specified auth.

By doing this, every API call made with this service client will use 'API KEY' to authenticate.

Note: Created clients are immutable so you must create a new one if you want to specify different options.

Example: Specifying default service client query parameters
const urlshortener = google.urlshortener({
  version: 'v1',
  params: { quotaUser: '[email protected]' }
});
// All requests made with this service client will contain the
// quotaUser query parameter unless overridden in individual API calls.

// Calls with this drive client will NOT contain the quotaUser query parameter.
const drive = google.drive('v3');

Request-level options

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:

const {google} = require('googleapis');
const bigquery = google.bigquery('v2');

// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
// environment variables.
google.auth.getApplicationDefault((err, authClient, projectId) => {
  if (err) {
    console.error('Authentication failed because of ', err);
    throw err;
  }

  if (authClient.createScopedRequired && authClient.createScopedRequired()) {
    const scopes = ['https://www.googleapis.com/auth/cloud-platform'];
    authClient = authClient.createScoped(scopes);
  }

  const request = {
    projectId: projectId,
    datasetId: '<YOUR_DATASET_ID>',

    // This is a "request-level" option
    auth: authClient
  };

  bigquery.datasets.delete(request, (err, response) => {
    if (err) {
      throw err;
    }
    console.log(response.data);
  });
});

You can also override axios options per request, such as url, method, and encoding.

For example:

drive.files.export({
  fileId: 'asxKJod9s79', // A Google Doc
  mimeType: 'application/pdf'
}, {
  encoding: null // Make sure we get the binary data
}, (err, response) => {
  // ...
});

Using a Proxy

You can use the following environment variables to proxy HTTP and HTTPS requests:

  • HTTP_PROXY / http_proxy
  • HTTPS_PROXY / https_proxy

When HTTP_PROXY / http_proxy are set, they will be used to proxy non-SSL requests that do not have an explicit proxy configuration option present. Similarly, HTTPS_PROXY / https_proxy will be respected for SSL requests that do not have an explicit proxy configuration option. It is valid to define a proxy in one of the environment variables, but then override it for a specific request, using the proxy configuration option.

License

This library is licensed under Apache 2.0. Full license text is available in COPYING.

Contributing

See CONTRIBUTING.

google-api-nodejs-client's People

Contributors

alexander-fenster avatar bantini avatar bentona avatar calebcase avatar erlichmen avatar glennschler avatar googleapis-publisher avatar greenkeeper[bot] avatar greenkeeperio-bot avatar ionicabizau avatar jasonall avatar jbergknoff avatar jmdobry avatar josephpage avatar justinbeckwith avatar monsur avatar ofrobots avatar proppy avatar qubyte avatar rakyll avatar readmecritic avatar robertrossmann avatar ryanseys avatar saicheems avatar sqrrrl avatar stephen avatar streamer45 avatar tamalsaha avatar tbetbetbe avatar vikramtiwari avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.