Skip to content

Commit e4e1ab5

Browse files
author
Eric Koleda
committed
Merge branch 'master' of github.com:googlesamples/apps-script-oauth2
2 parents 7f2ee1b + 1204a9a commit e4e1ab5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

samples/Basecamp.gs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Basecamp 3 API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var url = 'https://launchpad.37signals.com/authorization.json';
11+
var response = UrlFetchApp.fetch(url, {
12+
headers: {
13+
Authorization: 'Bearer ' + service.getAccessToken()
14+
}
15+
});
16+
var result = JSON.parse(response.getContentText());
17+
Logger.log(JSON.stringify(result, null, 2));
18+
} else {
19+
var authorizationUrl = service.getAuthorizationUrl();
20+
Logger.log('Open the following URL and re-run the script: %s',
21+
authorizationUrl);
22+
}
23+
}
24+
25+
/**
26+
* Reset the authorization state, so that it can be re-tested.
27+
*/
28+
function reset() {
29+
var service = getService();
30+
service.reset();
31+
}
32+
33+
/**
34+
* Configures the service.
35+
*/
36+
function getService() {
37+
return OAuth2.createService('Basecamp')
38+
// Set the endpoint URLs.
39+
.setAuthorizationBaseUrl('https://launchpad.37signals.com/authorization/new?type=web_server')
40+
.setTokenUrl('https://launchpad.37signals.com/authorization/token?type=web_server')
41+
42+
// Set the client ID and secret.
43+
.setClientId(CLIENT_ID)
44+
.setClientSecret(CLIENT_SECRET)
45+
46+
// Set the name of the callback function that should be invoked to complete
47+
// the OAuth flow.
48+
.setCallbackFunction('authCallback')
49+
50+
// Set the property store where authorized tokens should be persisted.
51+
.setPropertyStore(PropertiesService.getUserProperties())
52+
}
53+
54+
/**
55+
* Handles the OAuth callback.
56+
*/
57+
function authCallback(request) {
58+
var service = getService();
59+
var authorized = service.handleCallback(request);
60+
if (authorized) {
61+
return HtmlService.createHtmlOutput('Success!');
62+
} else {
63+
return HtmlService.createHtmlOutput('Denied');
64+
}
65+
}

src/Service.gs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ Service_.prototype.reset = function() {
373373
validate_({
374374
'Property store': this.propertyStore_
375375
});
376-
this.propertyStore_.deleteProperty(this.getPropertyKey_(this.serviceName_));
376+
var key = this.getPropertyKey_(this.serviceName_);
377+
this.propertyStore_.deleteProperty(key);
378+
if (this.cache_) {
379+
this.cache_.remove(key);
380+
}
377381
};
378382

379383
/**

0 commit comments

Comments
 (0)