npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-service-account

v1.0.3

Published

Create a authorized connection to Google APIs.

Downloads

173

Readme

google-service-account

Make authenticated requests using a Google service account.

This maintains an active Google service account token, allowing you to make server-to-server requests. It will automatically re-fetch a new token if a previous one expires.

To make a valid connection, you just attach an Authorization property to your outgoing request's headers object. This module returns that object.

Install

$ npm install --save google-service-account

Use

error handling omitted

var request = require("request")

var authorize = require("google-service-account")({
  keyFile: "path/to/keyfile.json"
})

authorize(function (err, headers) {
  request({
    method: "GET",
    uri: "https://www.googleapis.com/pubsub/v1beta1/subscriptions",
    headers: headers
  }, function () {
    // Request callback.
  })
})

Each time you invoke authorize(), a new token may be fetched if necessary. Be sure to call it each time you make an outgoing request.

If the example above looked a little verbose for you, you may also use the authorize function for its extending functionality:

authorize({
  method: "GET",
  uri: "https://www.googleapis.com/pubsub/v1beta1/subscriptions"
}, function (err, requestObject) {
  request(requestObject, function () {
    // Request callback.
  })
})

If even that gets tiresome, you can always write up a quick helper for your app:

function makeAuthorizedRequest(opts, cb) {
  authorize(opts, function (err, requestObject) {
    request(requestObject, cb)
  })
}
// ...later...
makeAuthorizedRequest({
  method: "GET",
  uri: "https://www.googleapis.com/pubsub/v1beta1/subscriptions"
}, function(err, response) {
  // Request callback.
})

API

var authorize = require("google-service-account")(options)

options

One of the following is required:

options.credentials
  • Type: Object

The contents of a JSON key file downloaded from the Google Developers Console.

options.keyFile
  • Type: String

Path to a JSON key file downloaded from the Google Developers Console.

options.scopes
  • Type: Array

The scopes your request requires.

authorize([options,] callback)

  • Type: Function

Invoke this method every time you need a valid token. It will handle requesting a new token if necessary, and will return it to your callback as part of an HTTP request headers object:

{
  headers: {
    Authorization: "..token.."
  }
}

options

  • (optional)
  • Type: Object

The object you pass in here will be extended with the token object in the format above.

callback

  • Type: Function

The callback function receives a HTTP request headers object, containing a valid token.

authorize.getCredentials()

Invoke this method to get the credentials object, containing the client_email, client_id, etc.

authorize.getToken()

Invoke this method to get only the value of a token.