From 38ae32e16ec91f0af02c1995c58ffc39896b076d Mon Sep 17 00:00:00 2001 From: Eric Koleda Date: Wed, 4 Mar 2015 16:49:09 -0500 Subject: [PATCH 1/2] Setting the Accepts HTTP header on token requests based on the token format. --- OAuth2.gs | 4 ++-- Service.gs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/OAuth2.gs b/OAuth2.gs index 1c6eab29..212bd4be 100644 --- a/OAuth2.gs +++ b/OAuth2.gs @@ -26,8 +26,8 @@ var _ = Underscore.load(); * @type {Object. */ var TOKEN_FORMAT = { - JSON: 'json', - FORM_URL_ENCODED: 'form-urlencoded' + JSON: 'application/json', + FORM_URL_ENCODED: 'application/x-www-form-urlencoded' }; /** diff --git a/Service.gs b/Service.gs index 0012f56a..d6bf66ad 100644 --- a/Service.gs +++ b/Service.gs @@ -60,8 +60,8 @@ Service_.prototype.setTokenUrl = function(tokenUrl) { }; /** - * Sets the format of the returned token. Default: Service_.TOKEN_FORMAT.JSON. - * @param {TOKEN_FORMAT} tokenFormat The format of the returned token. + * Sets the format of the returned token. Default: OAuth2.TOKEN_FORMAT.JSON. + * @param {OAuth2.TOKEN_FORMAT} tokenFormat The format of the returned token. * @return {Service_} This service, for chaining. */ Service_.prototype.setTokenFormat = function(tokenFormat) { @@ -226,6 +226,9 @@ Service_.prototype.handleCallback = function(callbackRequest) { var redirectUri = getRedirectUri(this.projectKey_); var response = UrlFetchApp.fetch(this.tokenUrl_, { method: 'post', + headers: { + 'Accept': this.tokenFormat_ + }, payload: { code: code, client_id: this.clientId_, @@ -235,6 +238,8 @@ Service_.prototype.handleCallback = function(callbackRequest) { }, muteHttpExceptions: true }); + Logger.log(response.getAllHeaders()); + Logger.log('Response:' + response.getContentText()); var token = this.parseToken_(response.getContentText()); if (response.getResponseCode() != 200) { var reason = token.error ? token.error : response.getResponseCode(); @@ -258,7 +263,7 @@ Service_.prototype.hasAccess = function() { var expires_in = token.expires_in || token.expires; if (expires_in) { var expires_time = token.granted_time + expires_in; - var now = getTimeInSeconds_(); + var now = getTimeInSeconds_(new Date()); if (expires_time - now < Service_.EXPIRATION_BUFFER_SECONDS_) { if (token.refresh_token) { this.refresh_(); @@ -317,7 +322,7 @@ Service_.prototype.parseToken_ = function(content) { } else { throw 'Unknown token format: ' + this.tokenFormat_; } - token.granted_time = getTimeInSeconds_(); + token.granted_time = getTimeInSeconds_(new Date()); return token; }; @@ -338,6 +343,9 @@ Service_.prototype.refresh_ = function() { } var response = UrlFetchApp.fetch(this.tokenUrl_, { method: 'post', + headers: { + 'Accept': this.tokenFormat_ + }, payload: { refresh_token: token.refresh_token, client_id: this.clientId_, @@ -410,4 +418,3 @@ Service_.prototype.getToken_ = function() { Service_.prototype.getPropertyKey = function(serviceName) { return 'oauth2.' + serviceName; }; - From 061b7b344b1b74d2cadbedb1d08a4d2b805f43a3 Mon Sep 17 00:00:00 2001 From: Eric Koleda Date: Wed, 4 Mar 2015 16:49:09 -0500 Subject: [PATCH 2/2] Setting the Accepts HTTP header on token requests based on the token format. --- OAuth2.gs | 4 ++-- Service.gs | 15 ++++++++++----- Utilities.gs | 8 +++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/OAuth2.gs b/OAuth2.gs index 1c6eab29..212bd4be 100644 --- a/OAuth2.gs +++ b/OAuth2.gs @@ -26,8 +26,8 @@ var _ = Underscore.load(); * @type {Object. */ var TOKEN_FORMAT = { - JSON: 'json', - FORM_URL_ENCODED: 'form-urlencoded' + JSON: 'application/json', + FORM_URL_ENCODED: 'application/x-www-form-urlencoded' }; /** diff --git a/Service.gs b/Service.gs index 0012f56a..d060c549 100644 --- a/Service.gs +++ b/Service.gs @@ -60,8 +60,8 @@ Service_.prototype.setTokenUrl = function(tokenUrl) { }; /** - * Sets the format of the returned token. Default: Service_.TOKEN_FORMAT.JSON. - * @param {TOKEN_FORMAT} tokenFormat The format of the returned token. + * Sets the format of the returned token. Default: OAuth2.TOKEN_FORMAT.JSON. + * @param {OAuth2.TOKEN_FORMAT} tokenFormat The format of the returned token. * @return {Service_} This service, for chaining. */ Service_.prototype.setTokenFormat = function(tokenFormat) { @@ -226,6 +226,9 @@ Service_.prototype.handleCallback = function(callbackRequest) { var redirectUri = getRedirectUri(this.projectKey_); var response = UrlFetchApp.fetch(this.tokenUrl_, { method: 'post', + headers: { + 'Accept': this.tokenFormat_ + }, payload: { code: code, client_id: this.clientId_, @@ -258,7 +261,7 @@ Service_.prototype.hasAccess = function() { var expires_in = token.expires_in || token.expires; if (expires_in) { var expires_time = token.granted_time + expires_in; - var now = getTimeInSeconds_(); + var now = getTimeInSeconds_(new Date()); if (expires_time - now < Service_.EXPIRATION_BUFFER_SECONDS_) { if (token.refresh_token) { this.refresh_(); @@ -317,7 +320,7 @@ Service_.prototype.parseToken_ = function(content) { } else { throw 'Unknown token format: ' + this.tokenFormat_; } - token.granted_time = getTimeInSeconds_(); + token.granted_time = getTimeInSeconds_(new Date()); return token; }; @@ -338,6 +341,9 @@ Service_.prototype.refresh_ = function() { } var response = UrlFetchApp.fetch(this.tokenUrl_, { method: 'post', + headers: { + 'Accept': this.tokenFormat_ + }, payload: { refresh_token: token.refresh_token, client_id: this.clientId_, @@ -410,4 +416,3 @@ Service_.prototype.getToken_ = function() { Service_.prototype.getPropertyKey = function(serviceName) { return 'oauth2.' + serviceName; }; - diff --git a/Utilities.gs b/Utilities.gs index 6ab7fd13..7ea2b56c 100644 --- a/Utilities.gs +++ b/Utilities.gs @@ -58,9 +58,11 @@ function isEmpty_(value) { } /** - * Gets the current time in seconds, rounded down to the nearest second. + * Gets the time in seconds, rounded down to the nearest second. + * @param {Date} date The Date object to convert. + * @returns {Number} The number of seconds since the epoch. * @private */ -function getTimeInSeconds_() { - return Math.floor(new Date().getTime() / 1000); +function getTimeInSeconds_(date) { + return Math.floor(date.getTime() / 1000); }