Get Google API access token from Google Cloud service account file/object.
- zero dependency
npm install --save @jeanycyang/google-api-access-token
const getAccessToken = require('@jeanycyang/google-api-access-token');
Function getAccessToken
returns a promise.
you can provide json file path or directly use object.
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 => { ... });
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 => { ... });
- https://developers.google.com/identity/protocols/OAuth2ServiceAccount
- https://developers.google.com/identity/protocols/googlescopes
MIT