Skip to content

Commit 38ae32e

Browse files
author
Eric Koleda
committed
Setting the Accepts HTTP header on token requests based on the token format.
1 parent a905451 commit 38ae32e

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

OAuth2.gs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var _ = Underscore.load();
2626
* @type {Object.<string, string>
2727
*/
2828
var TOKEN_FORMAT = {
29-
JSON: 'json',
30-
FORM_URL_ENCODED: 'form-urlencoded'
29+
JSON: 'application/json',
30+
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
3131
};
3232

3333
/**

Service.gs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Service_.prototype.setTokenUrl = function(tokenUrl) {
6060
};
6161

6262
/**
63-
* Sets the format of the returned token. Default: Service_.TOKEN_FORMAT.JSON.
64-
* @param {TOKEN_FORMAT} tokenFormat The format of the returned token.
63+
* Sets the format of the returned token. Default: OAuth2.TOKEN_FORMAT.JSON.
64+
* @param {OAuth2.TOKEN_FORMAT} tokenFormat The format of the returned token.
6565
* @return {Service_} This service, for chaining.
6666
*/
6767
Service_.prototype.setTokenFormat = function(tokenFormat) {
@@ -226,6 +226,9 @@ Service_.prototype.handleCallback = function(callbackRequest) {
226226
var redirectUri = getRedirectUri(this.projectKey_);
227227
var response = UrlFetchApp.fetch(this.tokenUrl_, {
228228
method: 'post',
229+
headers: {
230+
'Accept': this.tokenFormat_
231+
},
229232
payload: {
230233
code: code,
231234
client_id: this.clientId_,
@@ -235,6 +238,8 @@ Service_.prototype.handleCallback = function(callbackRequest) {
235238
},
236239
muteHttpExceptions: true
237240
});
241+
Logger.log(response.getAllHeaders());
242+
Logger.log('Response:' + response.getContentText());
238243
var token = this.parseToken_(response.getContentText());
239244
if (response.getResponseCode() != 200) {
240245
var reason = token.error ? token.error : response.getResponseCode();
@@ -258,7 +263,7 @@ Service_.prototype.hasAccess = function() {
258263
var expires_in = token.expires_in || token.expires;
259264
if (expires_in) {
260265
var expires_time = token.granted_time + expires_in;
261-
var now = getTimeInSeconds_();
266+
var now = getTimeInSeconds_(new Date());
262267
if (expires_time - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
263268
if (token.refresh_token) {
264269
this.refresh_();
@@ -317,7 +322,7 @@ Service_.prototype.parseToken_ = function(content) {
317322
} else {
318323
throw 'Unknown token format: ' + this.tokenFormat_;
319324
}
320-
token.granted_time = getTimeInSeconds_();
325+
token.granted_time = getTimeInSeconds_(new Date());
321326
return token;
322327
};
323328

@@ -338,6 +343,9 @@ Service_.prototype.refresh_ = function() {
338343
}
339344
var response = UrlFetchApp.fetch(this.tokenUrl_, {
340345
method: 'post',
346+
headers: {
347+
'Accept': this.tokenFormat_
348+
},
341349
payload: {
342350
refresh_token: token.refresh_token,
343351
client_id: this.clientId_,
@@ -410,4 +418,3 @@ Service_.prototype.getToken_ = function() {
410418
Service_.prototype.getPropertyKey = function(serviceName) {
411419
return 'oauth2.' + serviceName;
412420
};
413-

0 commit comments

Comments
 (0)