Skip to content

Commit 30e0f01

Browse files
author
Eric Koleda
committed
Add getRedirectUri() method and note the issue mismatched project keys. Fixes issue #44.
1 parent fc85bd5 commit 30e0f01

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ Where `{PROJECT KEY}` is the key of the script that is using this library. You
3838
can find your script's project key in the Apps Script code editor by clicking on
3939
the menu item "File > Project properties".
4040

41+
**Warning**: Due to an
42+
[open issue](https://code.google.com/p/google-apps-script-issues/issues/detail?id=6098)
43+
in Apps Script, the project key shown in the Project properties dialog
44+
doesn't match the version the library uses. You can call the service's
45+
`getRedirectUri()` method to view the exact URL that the service will use when
46+
performing the OAuth flow. Register this version in addition to or in place of
47+
the version that uses the key shown in the dialog.
4148

4249
## Usage
4350

@@ -190,8 +197,8 @@ See the [FitBit sample](samples/FitBit.gs) for the complete code.
190197

191198
#### Modifying the access token payload
192199
Similar to Setting additional token headers, some services, such as the Smartsheet API, require you to [add a hash to the access token request payloads](http://smartsheet-platform.github.io/api-docs/?javascript#oauth-flow). The `setTokenPayloadHandler` method allows you to pass in a function to modify the payload of an access token request before the request is sent to the token endpoint:
193-
194-
200+
201+
195202
// Set the handler for modifying the access token request payload:
196203
.setTokenPayloadHandler(myTokenHandler)
197204

samples/GooglePlus.gs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ function getService() {
3838
// Set the endpoint URLs.
3939
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
4040
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
41-
41+
4242
// Set the client ID and secret.
4343
.setClientId(CLIENT_ID)
4444
.setClientSecret(CLIENT_SECRET)
45-
45+
4646
// Set the name of the callback function that should be invoked to complete
4747
// the OAuth flow.
4848
.setCallbackFunction('authCallback')
4949

5050
// Set the property store where authorized tokens should be persisted.
5151
.setPropertyStore(PropertiesService.getUserProperties())
52-
52+
5353
// Set the scope and additional Google-specific parameters.
5454
.setScope('profile')
5555
.setParam('access_type', 'offline')
@@ -68,4 +68,12 @@ function authCallback(request) {
6868
} else {
6969
return HtmlService.createHtmlOutput('Denied');
7070
}
71-
}
71+
}
72+
73+
/**
74+
* Logs the redict URI to register in the Google Developers Console.
75+
*/
76+
function logRedirectUri() {
77+
var service = getService();
78+
Logger.log(service.getRedirectUri());
79+
}

src/Service.gs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Service_.prototype.setTokenHeaders = function(tokenHeaders) {
9494
*/
9595
Service_.prototype.setTokenPayloadHandler = function(tokenHandler) {
9696
this.tokenPayloadHandler_ = tokenHandler;
97-
return this;
97+
return this;
9898
};
9999

100100
/**
@@ -257,7 +257,7 @@ Service_.prototype.getAuthorizationUrl = function() {
257257
'Authorization base URL': this.authorizationBaseUrl_
258258
});
259259

260-
var redirectUri = getRedirectUri(this.projectKey_);
260+
var redirectUri = this.getRedirectUri();
261261
var state = eval('Script' + 'App').newStateToken()
262262
.withMethod(this.callbackFunctionName_)
263263
.withArgument('serviceName', this.serviceName_)
@@ -294,7 +294,7 @@ Service_.prototype.handleCallback = function(callbackRequest) {
294294
'Project key': this.projectKey_,
295295
'Token URL': this.tokenUrl_
296296
});
297-
var redirectUri = getRedirectUri(this.projectKey_);
297+
var redirectUri = this.getRedirectUri();
298298
var headers = {
299299
'Accept': this.tokenFormat_
300300
};
@@ -386,11 +386,21 @@ Service_.prototype.getLastError = function() {
386386
return this.lastError_;
387387
};
388388

389+
/**
390+
* Gets the last error that occurred this execution when trying to automatically refresh
391+
* or generate an access token.
392+
* @return {Exception} An error, if any.
393+
*/
394+
Service_.prototype.getRedirectUri = function() {
395+
return getRedirectUri(this.projectKey_);
396+
};
397+
389398
/**
390399
* Gets the token from a UrlFetchApp response.
391400
* @param {UrlFetchApp.HTTPResponse} response The response object.
392401
* @return {Object} The parsed token.
393402
* @throws If the token cannot be parsed or the response contained an error.
403+
* @private
394404
*/
395405
Service_.prototype.getTokenFromResponse_ = function(response) {
396406
var token = this.parseToken_(response.getContentText());

0 commit comments

Comments
 (0)