3.0.2 • Published 7 years ago
@leonardodino/gtoken v3.0.2
node-gtoken
Node.js Google Authentication Service Account Tokens
Installation
npm install @leonardodino/gtokenor
yarn add @leonardodino/gtokenUsage
const key = '-----BEGIN RSA PRIVATE KEY-----\nXXXXXXXXXXX...';
const { GoogleToken } = require('@leonardodino/gtoken');
const gtoken = new GoogleToken({
email: 'my_service_account_email@developer.gserviceaccount.com',
scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes
key: key
});Options
Various options that can be set when creating initializing the
gtokenobject.
options.email or options.iss: The service account email address.options.scope: An array of scope strings or space-delimited string of scopes.options.sub: The email address of the user requesting delegated access.options.key: The raw RSA private key value.
.getToken()
Returns the cached token or requests a new one and returns it.
gtoken.getToken(function(err, token) {
if (err) {
console.log(err);
return;
}
console.log(token);
});You can also use the async/await style API:
const token = await gtoken.getToken()
console.log(token);Or use promises: or
gtoken.getToken()
.then(token => {
console.log(token)
})
.catch(e => console.error);Properties
Various properties set on the gtoken object after call to
.getToken().
gtoken.token: The access token.gtoken.expiresAt: The expiry date as milliseconds since 1970/01/01gtoken.key: The raw key value.gtoken.rawToken: Most recent raw token data received from Google.
.hasExpired()
Returns true if the token has expired, or token does not exist.
gtoken.getToken(function(err, token) {
if(token) {
gtoken.hasExpired(); // false
}
});.revokeToken()
Revoke the token if set.
gtoken.revokeToken(function(err) {
if (err) {
console.log(err);
return;
}
console.log('Token revoked!');
});Changelog
2.0.0 -> 3.0.0
New features:
- Fewer dependencies, code works everywhere.
Breaking changes:
- remove support for reading files (
keyFileoption) - remove
getCredentialsmethod. - remove
ensureEmailprivate method.
1.2.2 -> 2.0.0
New features:
- API now supports callback and promise based workflows
Breaking changes:
GoogleTokenis now a class type, and must be instantiated.GoogleToken.expires_atrenamed toGoogleToken.expiresAtGoogleToken.raw_tokenrenamed toGoogleToken.rawTokenGoogleToken.token_expiresrenamed toGoogleToken.tokenExpires