Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OAuth2.gs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var _ = Underscore.load();
* @type {Object.<string, string>
*/
var TOKEN_FORMAT = {
JSON: 'json',
FORM_URL_ENCODED: 'form-urlencoded'
JSON: 'application/json',
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
};

/**
Expand Down
15 changes: 10 additions & 5 deletions Service.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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_,
Expand Down Expand Up @@ -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_();
Expand Down Expand Up @@ -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;
};

Expand All @@ -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_,
Expand Down Expand Up @@ -410,4 +416,3 @@ Service_.prototype.getToken_ = function() {
Service_.prototype.getPropertyKey = function(serviceName) {
return 'oauth2.' + serviceName;
};

8 changes: 5 additions & 3 deletions Utilities.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}