Skip to content

Commit 2c20b21

Browse files
author
Eric Koleda
committed
Expose the ability to manually refresh tokens, for APIs that provide refresh_tokens but don't set an expiry time (SalesForce for instance). Fixes googleworkspace#10.
1 parent b712357 commit 2c20b21

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

Service.gs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var Service_ = function(serviceName) {
2828
this.serviceName_ = serviceName;
2929
this.params_ = {};
3030
this.tokenFormat_ = TOKEN_FORMAT.JSON;
31+
this.stateParameterLocation_ = STATE_PARAMETER_LOCATION.AUTHORIZATION_URL;
3132
};
3233

3334
/**
@@ -69,6 +70,18 @@ Service_.prototype.setTokenFormat = function(tokenFormat) {
6970
return this;
7071
};
7172

73+
/**
74+
* Sets the location of the state parameter, which is required by the
75+
* /usercallback endpoint.
76+
* Default: OAuth2.STATE_PARAMETER_LOCATION.AUTHORIZATION_URL.
77+
* @param {STATE_PARAMETER_LOCATION} tokenFormat The format of the returned token.
78+
* @return {Service_} This service, for chaining.
79+
*/
80+
Service_.prototype.setStateParameterLocation = function(stateParameterLocation) {
81+
this.stateParameterLocation_ = stateParameterLocation
82+
return this;
83+
};
84+
7285
/**
7386
* Sets the project key of the script that contains the authorization callback function (required).
7487
* The project key can be found in the Script Editor UI under "File > Project properties".
@@ -194,10 +207,16 @@ Service_.prototype.getAuthorizationUrl = function() {
194207
.createToken();
195208
var params = {
196209
client_id: this.clientId_,
197-
response_type: 'code',
198-
redirect_uri: redirectUri,
199-
state: state
210+
response_type: 'code'
200211
};
212+
if (this.stateParameterLocation_ == STATE_PARAMETER_LOCATION.AUTHORIZATION_URL) {
213+
params['state'] = state;
214+
} else {
215+
redirectUri = buildUrl_(redirectUri, {
216+
state: state
217+
});
218+
}
219+
params['redirect_uri'] = redirectUri;
201220
params = _.extend(params, this.params_);
202221
return buildUrl_(this.authorizationBaseUrl_, params);
203222
};
@@ -327,9 +346,8 @@ Service_.prototype.parseToken_ = function(content) {
327346
/**
328347
* Refreshes a token that has expired. This is only possible if offline access was
329348
* requested when the token was authorized.
330-
* @private
331349
*/
332-
Service_.prototype.refresh_ = function() {
350+
Service_.prototype.refresh = function() {
333351
validate_({
334352
'Client ID': this.clientId_,
335353
'Client Secret': this.clientSecret_,

0 commit comments

Comments
 (0)