Skip to content

Commit 96f2709

Browse files
committed
Add Strava API example
1 parent 76a980d commit 96f2709

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

samples/Strava.gs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var CLIENT_ID;
2+
var CLIENT_SECRET;
3+
4+
/**
5+
* Authorizes and makes a request to the Strava API.
6+
*/
7+
function run() {
8+
var service = getService_();
9+
if (service.hasAccess()) {
10+
var url = 'https://www.strava.com/api/v3/activities';
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+
* Three required and optional parameters are not specified
36+
* because the library creates the authorization URL with them
37+
* automatically: `redirect_url`, `response_type`, and
38+
* `state`.
39+
*/
40+
function getService_() {
41+
42+
return OAuth2.createService('Strava')
43+
// Set the endpoint URLs.
44+
.setAuthorizationBaseUrl('https://www.strava.com/oauth/authorize')
45+
.setTokenUrl('https://www.strava.com/oauth/token')
46+
47+
// Set the client ID and secret.
48+
.setClientId(CLIENT_ID)
49+
.setClientSecret(CLIENT_SECRET)
50+
51+
// Set the name of the callback function that should be invoked to complete
52+
// the OAuth flow.
53+
.setCallbackFunction('authCallback_')
54+
55+
// Set the property store where authorized tokens should be persisted.
56+
.setPropertyStore(PropertiesService.getUserProperties());
57+
}
58+
59+
/**
60+
* Handles the OAuth callback.
61+
*/
62+
function authCallback_(request) {
63+
var service = getService_();
64+
var authorized = service.handleCallback(request);
65+
if (authorized) {
66+
return HtmlService.createHtmlOutput('Success!');
67+
} else {
68+
return HtmlService.createHtmlOutput('Denied');
69+
}
70+
}

0 commit comments

Comments
 (0)