Skip to content

Commit f6fed2e

Browse files
author
Eric Koleda
authored
Merge pull request #72 from stvkas/master
Fix Bug in `getTokenFromResponse_` for APIs that Respond with 201 Created
2 parents 6eedf05 + 96f2709 commit f6fed2e

File tree

4 files changed

+152
-6
lines changed

4 files changed

+152
-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
}

samples/Medium.gs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var CLIENT_ID;
2+
var CLIENT_SECRET;
3+
4+
/**
5+
* Authorizes and makes a request to the Medium API.
6+
*/
7+
function run() {
8+
var service = getService_();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.medium.com/v1/me';
11+
var response = UrlFetchApp.fetch(url, {
12+
headers: {
13+
Authorization: 'Bearer ' + service.getAccessToken()
14+
}
15+
});
16+
var result = JSON.parse(response.getContentText());
17+
Logger.log(JSON.stringify(result, null, 2));
18+
} else {
19+
var authorizationUrl = service.getAuthorizationUrl();
20+
Logger.log('Open the following URL and re-run the script: %s',
21+
authorizationUrl);
22+
}
23+
}
24+
25+
/**
26+
* Reset the authorization state, so that it can be re-tested.
27+
*/
28+
function reset() {
29+
var service = getService_();
30+
service.reset();
31+
}
32+
33+
34+
/**
35+
* Configures the service.
36+
* Three required parameters are not specified because
37+
* the library creates the authorization URL with them
38+
* automatically: `redirect_url`, `response_type`, and
39+
* `state`.
40+
*/
41+
function getService_() {
42+
43+
return OAuth2.createService('Medium')
44+
// Set the endpoint URLs.
45+
.setAuthorizationBaseUrl('https://medium.com/m/oauth/authorize')
46+
.setTokenUrl('https://api.medium.com/v1/tokens')
47+
48+
// Set the client ID and secret.
49+
.setClientId(CLIENT_ID)
50+
.setClientSecret(CLIENT_SECRET)
51+
52+
// Set the name of the callback function that should be invoked to complete
53+
// the OAuth flow.
54+
.setCallbackFunction('authCallback_')
55+
56+
// Set the property store where authorized tokens should be persisted.
57+
.setPropertyStore(PropertiesService.getUserProperties())
58+
59+
// Set scope (required)
60+
.setScope('basicProfile');
61+
}
62+
63+
/**
64+
* Handles the OAuth callback.
65+
*/
66+
function authCallback_(request) {
67+
var service = getService_();
68+
var authorized = service.handleCallback(request);
69+
if (authorized) {
70+
return HtmlService.createHtmlOutput('Success!');
71+
} else {
72+
return HtmlService.createHtmlOutput('Denied');
73+
}
74+
}

samples/Strava.gs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var CLIENT_ID;
2+
var CLIENT_SECRET;
3+
4+
/**
5+
* Authorizes and makes a request to the Strava API.
6+
*/
7+
function run() {
8+
var service = getService_();
9+
if (service.hasAccess()) {
10+
var url = 'https://www.strava.com/api/v3/activities';
11+
var response = UrlFetchApp.fetch(url, {
12+
headers: {
13+
Authorization: 'Bearer ' + service.getAccessToken()
14+
}
15+
});
16+
var result = JSON.parse(response.getContentText());
17+
Logger.log(JSON.stringify(result, null, 2));
18+
} else {
19+
var authorizationUrl = service.getAuthorizationUrl();
20+
Logger.log('Open the following URL and re-run the script: %s',
21+
authorizationUrl);
22+
}
23+
}
24+
25+
/**
26+
* Reset the authorization state, so that it can be re-tested.
27+
*/
28+
function reset() {
29+
var service = getService_();
30+
service.reset();
31+
}
32+
33+
/**
34+
* Configures the service.
35+
* Three required and optional parameters are not specified
36+
* because the library creates the authorization URL with them
37+
* automatically: `redirect_url`, `response_type`, and
38+
* `state`.
39+
*/
40+
function getService_() {
41+
42+
return OAuth2.createService('Strava')
43+
// Set the endpoint URLs.
44+
.setAuthorizationBaseUrl('https://www.strava.com/oauth/authorize')
45+
.setTokenUrl('https://www.strava.com/oauth/token')
46+
47+
// Set the client ID and secret.
48+
.setClientId(CLIENT_ID)
49+
.setClientSecret(CLIENT_SECRET)
50+
51+
// Set the name of the callback function that should be invoked to complete
52+
// the OAuth flow.
53+
.setCallbackFunction('authCallback_')
54+
55+
// Set the property store where authorized tokens should be persisted.
56+
.setPropertyStore(PropertiesService.getUserProperties());
57+
}
58+
59+
/**
60+
* Handles the OAuth callback.
61+
*/
62+
function authCallback_(request) {
63+
var service = getService_();
64+
var authorized = service.handleCallback(request);
65+
if (authorized) {
66+
return HtmlService.createHtmlOutput('Success!');
67+
} else {
68+
return HtmlService.createHtmlOutput('Denied');
69+
}
70+
}

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)