node-gtoken
Node.js Google Authentication Service Account Tokens
中国版说明
由于中国国内不能直接访问googleapis服务,所以修改了原来的node-gtoken这个包,使之能够通过https.Agent
代理访问googleapis服务。具体变化如下:
- 删除了axios。虽然axios官方文档上支持使用
axios.defaults[...]
来设置全局属性,但实际使用失败,axios本身bug也很多。 - 添加 node-fetch 和 @types/node-fetch
- 给
GoogleToken
添加agent
参数
示例:
const HttpsProxyAgent = const agent = processenvHTTP_PROXYconst gtoken = agent iss: ISS scope: 'https://www.googleapis.com/auth/firebase.messaging' key: KEY
Installation
npm install gtoken
Usage
.pem
or .p12
key file:
Use with a const GoogleToken = ;const gtoken =keyFile: 'path/to/key.pem' // or path to .p12 key fileemail: 'my_service_account_email@developer.gserviceaccount.com'scope: 'https://scope1' 'https://scope2' // or space-delimited string of scopes;gtoken;
You can also use the async/await style API:
const token = await gtokenconsole;
Or use promises:
gtoken ;
.json
key file:
Use with a service account const GoogleToken = ;const gtoken =keyFile: 'path/to/key.json'scope: 'https://scope1' 'https://scope2' // or space-delimited string of scopes;gtoken;
Pass the private key as a string directly:
const key = '-----BEGIN RSA PRIVATE KEY-----\nXXXXXXXXXXX...';const GoogleToken = ;const gtoken =email: 'my_service_account_email@developer.gserviceaccount.com'scope: 'https://scope1' 'https://scope2' // or space-delimited string of scopeskey: key;
Options
Various options that can be set when creating initializing the
gtoken
object.
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.keyFile
: The filename of.json
key,.pem
key or.p12
key.options.key
: The raw RSA private key value, in place of usingoptions.keyFile
.
.getToken(callback)
Returns the cached token or requests a new one and returns it.
gtoken;
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;
.revokeToken()
Revoke the token if set.
gtoken;
.p12
key from Google
Downloading your private - Open the Google Developer Console.
- Open your project and under "APIs & auth", click Credentials.
- Generate a new
.p12
key and download it into your project.
.p12
key to a .pem
key
Converting your You can just specify your .p12
file (with .p12
extension) as the keyFile
and it will automatically be converted to a .pem
on the fly, however this results in a slight performance hit. If you'd like to convert to a .pem
for use later, use OpenSSL if you have it installed.
$ openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem
Don't forget, the passphrase when converting these files is the string 'notasecret'
Changelog
1.2.2 -> 2.0.0
New features:
- API now supports callback and promise based workflows
Breaking changes:
GoogleToken
is now a class type, and must be instantiated.GoogleToken.expires_at
renamed toGoogleToken.expiresAt
GoogleToken.raw_token
renamed toGoogleToken.rawToken
GoogleToken.token_expires
renamed toGoogleToken.tokenExpires