Git Product home page Git Product logo

google-auth-library-nodejs's Introduction

Google APIs Node.js Client

Build Status Code Coverage

This is Google's officially supported node.js client library for using OAuth 2.0 authorization and authentication with Google APIs.

Questions/problems?

  • Ask your development related questions on Ask a question on Stackoverflow
  • If you've found an bug/issue, please file it on GitHub.

Installation

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

$ npm install google-auth-library --save

Application Default Credentials

This library provides an implementation of Application Default Credentials for Node.js.

The Application Default Credentials provide a simple way to get authorization credentials for use in calling Google APIs.

They are best suited for cases when the call needs to have the same identity and authorization level for the application independent of the user. This is the recommended approach to authorize calls to Cloud APIs, particularly when you're building an application that uses Google Compute Engine.

Download your Service Account Credentials JSON file

To use Application Default Credentials, You first need to download a set of JSON credentials for your project. Go to APIs & Auth > Credentials in the [Google Developers Console](developer console) and select Service account from the Add credentials dropdown.

This file is your only copy of these credentials. It should never be committed with your source code, and should be stored securely.

Once downloaded, store the path to this file in the GOOGLE_APPLICATION_CREDENTIALS environment variable.

Enable the API you want to use

Before making your API call, you must be sure the API you're calling has been enabled. Go to APIs & Auth > APIs in the [Google Developers Console](developer console) and enable the APIs you'd like to call. For the example below, you must enable the DNS API.

Call an API

As long as you update the environment variable below to point to your JSON credentials file, and the fill in the placeholder variables from your project, the following snippet should work.

var google = require('googleapis');
var GoogleAuth = require('google-auth-library');

var authFactory = new GoogleAuth();
var dns = google.dns('v1');

authFactory.getApplicationDefault(function(err, authClient) {
  if (err) {
    console.log('Authentication failed because of ', err);
    return;
  }
  if (authClient.createScopedRequired && authClient.createScopedRequired()) {
    var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
    authClient = authClient.createScoped(scopes);
  }

  var request = {
    // TODO: Change placeholders below to values for parameters to the 'get' method:

    // Identifies the project addressed by this request.
    project: "",
    // Identifies the managed zone addressed by this request. Can be the managed zone name or id.
    managedZone: "",
    // The identifier of the requested change, from a previous ResourceRecordSetsChangeResponse.
    changeId: "",
    // Auth client
    auth: authClient
  };

  dns.changes.get(request, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
  });
});

Contributing

See CONTRIBUTING.

License

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

google-auth-library-nodejs's People

Contributors

fiznool avatar fossamagna avatar jasonall avatar josephpage avatar mcduan avatar murgatroid99 avatar ofrobots avatar riblee avatar ryan-devrel avatar ryanseys avatar sammcveety avatar tbetbetbe avatar timbertson 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.