-
Notifications
You must be signed in to change notification settings - Fork 25
/
create_access_token.js
36 lines (31 loc) · 1.05 KB
/
create_access_token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Node.js script for generating OAuth 2.0 bearer/access_token
//
// * dependencies: install npm package googleapis, i.e. run:
//
// npm install googleapis
//
// * set the path to your API secret (see comments below)
// * run script, and use generated access token from console output
//
// node create_access_token.js
//
// * additional information:
// * [Service accounts] https://cloud.google.com/docs/authentication/
// * https://github.com/google/google-api-nodejs-client#using-jwt-service-tokens
var google = require('googleapis');
var key = require('/path/to/key.json');//set path to JSON file with your API secret (see Google docs about authentification)
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/cloud-platform'],
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
var cred = jwtClient.credentials;
console.log('use authentification: '+cred.token_type+' '+cred.access_token);
});