Skip to content

Commit ca930e1

Browse files
committed
Return parsed token for all response status codes in the success range
Fixes a bug where the token request responds with a status code in the 2xx range and greater than 200. This modification accommodates authentication workflows that respond with 201 Created for token requests, such as Medium.
1 parent 6eedf05 commit ca930e1

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

dist/OAuth2.gs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
/**
2929
* The supported formats for the returned OAuth2 token.
30-
* @type {Object.<string, string>
30+
* @type {Object.<string, string>}
3131
*/
3232
var TOKEN_FORMAT = {
3333
JSON: 'application/json',
@@ -65,7 +65,7 @@ function getRedirectUri(scriptId) {
6565
return Utilities.formatString('https://script.google.com/macros/d/%s/usercallback', scriptId);
6666
}
6767

68-
if (module) {
68+
if (typeof module === 'object') {
6969
module.exports = {
7070
createService: createService,
7171
getRedirectUri: getRedirectUri
@@ -466,7 +466,8 @@ Service_.prototype.getRedirectUri = function() {
466466
*/
467467
Service_.prototype.getTokenFromResponse_ = function(response) {
468468
var token = this.parseToken_(response.getContentText());
469-
if (response.getResponseCode() != 200 || token.error) {
469+
var resCode = response.getResponseCode();
470+
if ( !(resCode >= 200 && resCode < 300) || token.error) {
470471
var reason = [
471472
token.error,
472473
token.message,
@@ -476,7 +477,7 @@ Service_.prototype.getTokenFromResponse_ = function(response) {
476477
return typeof(part) == 'string' ? part : JSON.stringify(part);
477478
}).join(', ');
478479
if (!reason) {
479-
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
480+
reason = resCode + ': ' + JSON.stringify(token);
480481
}
481482
throw 'Error retrieving token: ' + reason;
482483
}

src/Service.gs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ Service_.prototype.getRedirectUri = function() {
392392
*/
393393
Service_.prototype.getTokenFromResponse_ = function(response) {
394394
var token = this.parseToken_(response.getContentText());
395-
if (response.getResponseCode() != 200 || token.error) {
395+
var resCode = response.getResponseCode();
396+
if ( resCode < 200 || resCode >= 300 || token.error) {
396397
var reason = [
397398
token.error,
398399
token.message,
@@ -402,7 +403,7 @@ Service_.prototype.getTokenFromResponse_ = function(response) {
402403
return typeof(part) == 'string' ? part : JSON.stringify(part);
403404
}).join(', ');
404405
if (!reason) {
405-
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
406+
reason = resCode + ': ' + JSON.stringify(token);
406407
}
407408
throw 'Error retrieving token: ' + reason;
408409
}

0 commit comments

Comments
 (0)