Skip to content

get google api access token from service-account json file or object

License

Notifications You must be signed in to change notification settings

jeanycyang/google-api-access-token

Repository files navigation

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

About

get google api access token from service-account json file or object

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published