Skip to content

Latest commit

 

History

History
81 lines (63 loc) · 2.76 KB

README.md

File metadata and controls

81 lines (63 loc) · 2.76 KB

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": "[email protected]",
  "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": "[email protected]",
  "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