@jeanycyang/google-api-access-token

1.1.0 • Public • Published

Google API Access Token

npm version Build Status Coverage Status

Get Google API access token from Google Cloud service account file/object.

Features

  • zero dependency

Install

npm install --save @jeanycyang/google-api-access-token

Usage

const getAccessToken = require('@jeanycyang/google-api-access-token');

Function getAccessToken returns a promise.

you can provide json file path or directly use object.

from a json file

const accessToken = await getAccessToken('./config/gckey.json', 'https://www.googleapis.com/drive/v2/files');

or

getAccessToken('./config/gckey.json', 'https://www.googleapis.com/drive/v2/files')
  .then(accessToken => { ... });

from google cloud key object

const accessToken = await getAccessToken({
    "type": "service_account",
  "project_id": "gcp-project-1245",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----....",
  "client_email": "user-name@gcp-project-1245.iam.gserviceaccount.com",
  "client_id": "1234874585712327208344",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/user-name%40gcp-project-1245.iam.gserviceaccount.com"
}, 'https://www.googleapis.com/drive/v2/files');

or

getAccessToken({
  "type": "service_account",
  "project_id": "gcp-project-1245",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----....",
  "client_email": "user-name@gcp-project-1245.iam.gserviceaccount.com",
  "client_id": "1234874585712327208344",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/user-name%40gcp-project-1245.iam.gserviceaccount.com"
}, 'https://www.googleapis.com/drive/v2/files')
  .then(accessToken => { ... });

References

License

MIT

Package Sidebar

Install

npm i @jeanycyang/google-api-access-token

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

14.1 kB

Total Files

12

Last publish

Collaborators

  • jeanycyang