Skip to content

Commit b712357

Browse files
author
Eric Koleda
committed
Merge pull request googleworkspace#7 from erickoledadevrel/ref/heads/master
Setting the Accepts HTTP header on token requests based on the token format.
2 parents a905451 + d48962f commit b712357

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
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: 10 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_,
@@ -258,7 +261,7 @@ Service_.prototype.hasAccess = function() {
258261
var expires_in = token.expires_in || token.expires;
259262
if (expires_in) {
260263
var expires_time = token.granted_time + expires_in;
261-
var now = getTimeInSeconds_();
264+
var now = getTimeInSeconds_(new Date());
262265
if (expires_time - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
263266
if (token.refresh_token) {
264267
this.refresh_();
@@ -317,7 +320,7 @@ Service_.prototype.parseToken_ = function(content) {
317320
} else {
318321
throw 'Unknown token format: ' + this.tokenFormat_;
319322
}
320-
token.granted_time = getTimeInSeconds_();
323+
token.granted_time = getTimeInSeconds_(new Date());
321324
return token;
322325
};
323326

@@ -338,6 +341,9 @@ Service_.prototype.refresh_ = function() {
338341
}
339342
var response = UrlFetchApp.fetch(this.tokenUrl_, {
340343
method: 'post',
344+
headers: {
345+
'Accept': this.tokenFormat_
346+
},
341347
payload: {
342348
refresh_token: token.refresh_token,
343349
client_id: this.clientId_,
@@ -410,4 +416,3 @@ Service_.prototype.getToken_ = function() {
410416
Service_.prototype.getPropertyKey = function(serviceName) {
411417
return 'oauth2.' + serviceName;
412418
};
413-

Utilities.gs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ function isEmpty_(value) {
5858
}
5959

6060
/**
61-
* Gets the current time in seconds, rounded down to the nearest second.
61+
* Gets the time in seconds, rounded down to the nearest second.
62+
* @param {Date} date The Date object to convert.
63+
* @returns {Number} The number of seconds since the epoch.
6264
* @private
6365
*/
64-
function getTimeInSeconds_() {
65-
return Math.floor(new Date().getTime() / 1000);
66+
function getTimeInSeconds_(date) {
67+
return Math.floor(date.getTime() / 1000);
6668
}

0 commit comments

Comments
 (0)