Skip to content

Commit d077992

Browse files
author
Eric Koleda
committed
Add more detail to the error messages thrown when failing to retrieve a token.
1 parent 76a6b0d commit d077992

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Service.gs

+17-8
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Service_.prototype.setTokenFormat = function(tokenFormat) {
7373
};
7474

7575
/**
76-
* Sets the additional HTTP headers that should be sent when retrieving or
76+
* Sets the additional HTTP headers that should be sent when retrieving or
7777
* refreshing the access token.
7878
* @param Object.<string,string> tokenHeaders A map of header names to values.
7979
* @return {Service_} This service, for chaining.
@@ -197,7 +197,7 @@ Service_.prototype.setPrivateKey = function(privateKey) {
197197
};
198198

199199
/**
200-
* Sets the issuer (iss) value to use for Service Account authorization.
200+
* Sets the issuer (iss) value to use for Service Account authorization.
201201
* If not set the client ID will be used instead.
202202
* @param {string} issuer This issuer value
203203
* @return {Service_} This service, for chaining.
@@ -208,7 +208,7 @@ Service_.prototype.setIssuer = function(issuer) {
208208
};
209209

210210
/**
211-
* Sets the subject (sub) value to use for Service Account authorization.
211+
* Sets the subject (sub) value to use for Service Account authorization.
212212
* @param {string} subject This subject value
213213
* @return {Service_} This service, for chaining.
214214
*/
@@ -301,7 +301,10 @@ Service_.prototype.handleCallback = function(callbackRequest) {
301301
});
302302
var token = this.parseToken_(response.getContentText());
303303
if (response.getResponseCode() != 200) {
304-
var reason = token.error ? token.error : response.getResponseCode();
304+
var reason = token.error;
305+
if (!reason) {
306+
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
307+
}
305308
throw 'Error retrieving token: ' + reason;
306309
}
307310
this.saveToken_(token);
@@ -431,8 +434,11 @@ Service_.prototype.refresh = function() {
431434
});
432435
var newToken = this.parseToken_(response.getContentText());
433436
if (response.getResponseCode() != 200) {
434-
var reason = newToken.error ? newToken.error : response.getResponseCode();
435-
throw 'Error refreshing token: ' + reason;
437+
var reason = newToken.error;
438+
if (!reason) {
439+
reason = response.getResponseCode() + ': ' + JSON.stringify(newToken);
440+
}
441+
throw 'Error retrieving token: ' + reason;
436442
}
437443
if (!newToken.refresh_token) {
438444
newToken.refresh_token = token.refresh_token;
@@ -537,7 +543,10 @@ Service_.prototype.exchangeJwt_ = function() {
537543
});
538544
var token = this.parseToken_(response.getContentText());
539545
if (response.getResponseCode() != 200) {
540-
var reason = token.error ? token.error : response.getResponseCode();
546+
var reason = token.error;
547+
if (!reason) {
548+
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
549+
}
541550
throw 'Error retrieving token: ' + reason;
542551
}
543552
this.saveToken_(token);
@@ -577,4 +586,4 @@ Service_.prototype.createJwt_ = function() {
577586
var signatureBytes = Utilities.computeRsaSha256Signature(toSign, this.privateKey_);
578587
var signature = Utilities.base64EncodeWebSafe(signatureBytes);
579588
return toSign + '.' + signature;
580-
};
589+
};

0 commit comments

Comments
 (0)