File tree 1 file changed +22
-0
lines changed
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ var GAPIToken = require('gapitoken'),
18
18
async = require ( 'async' ) ,
19
19
req = require ( 'request' ) ;
20
20
21
+ var metadataTokenUrl = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token' ;
22
+
21
23
/**
22
24
* Token represents an access token
23
25
* @param {string } accessToken Access token.
@@ -80,6 +82,26 @@ Connection.prototype.connect = function(callback) {
80
82
*/
81
83
Connection . prototype . fetchToken = function ( callback ) {
82
84
var that = this ;
85
+ if ( ! this . email || ! this . privateKey ) {
86
+ // We should be on GCE, try to retrieve token from
87
+ // the metadata from server.
88
+ req ( {
89
+ method : 'get' ,
90
+ uri : metadataTokenUrl ,
91
+ headers : {
92
+ 'X-Google-Metadata-Request' : 'True'
93
+ } ,
94
+ json : true
95
+ } , function ( err , res , body ) {
96
+ if ( err || ! body . access_token ) {
97
+ // TODO: Provide better context about the error here.
98
+ return callback ( err ) ;
99
+ }
100
+ var exp = new Date ( body . token_expires * 1000 ) ;
101
+ callback ( null , new Token ( body . access_token , exp ) ) ;
102
+ } ) ;
103
+ return ;
104
+ }
83
105
var gapi = new GAPIToken ( {
84
106
iss : this . email ,
85
107
keyFile : this . privateKey ,
You can’t perform that action at this time.
0 commit comments