Skip to content

Commit 98da500

Browse files
author
Eric Koleda
committed
Added FitBit sample.
1 parent d077992 commit 98da500

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

samples/FitBit.gs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the FitBit API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.fitbit.com/1/user/-/profile.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('FitBit')
38+
// Set the endpoint URLs.
39+
.setAuthorizationBaseUrl('https://www.fitbit.com/oauth2/authorize')
40+
.setTokenUrl('https://api.fitbit.com/oauth2/token')
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
47+
// complete the OAuth flow.
48+
.setCallbackFunction('authCallback')
49+
50+
// Set the property store where authorized tokens should be persisted.
51+
.setPropertyStore(PropertiesService.getUserProperties())
52+
53+
// Set the scope and additional headers required by the FitBit API.
54+
.setScope('profile')
55+
.setTokenHeaders({
56+
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
57+
});
58+
}
59+
60+
/**
61+
* Handles the OAuth callback.
62+
*/
63+
function authCallback(request) {
64+
var service = getService();
65+
var authorized = service.handleCallback(request);
66+
if (authorized) {
67+
return HtmlService.createHtmlOutput('Success!');
68+
} else {
69+
return HtmlService.createHtmlOutput('Denied');
70+
}
71+
}

0 commit comments

Comments
 (0)