-
Notifications
You must be signed in to change notification settings - Fork 152
/
fitbit.gs
121 lines (107 loc) · 3.68 KB
/
fitbit.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fitbit = {
"serviceName": "Fitbit",
"clientId": PropertiesService.getScriptProperties().getProperty('FITBIT_API_CLIENT_ID'),
"clientSecret": PropertiesService.getScriptProperties().getProperty('FITBIT_API_CLIENT_SECRET'),
"setAuthorizationBaseUrl": "https://www.fitbit.com/oauth2/authorize",
"tokenUrl": "https://api.fitbit.com/oauth2/token",
"dataSourceUrl": "https://www.googleapis.com/fitness/v1/users/me/dataSources",
"callback": "fbAuthCallback",
"scope": "weight"
}
/**
* Fitbit用の認証サービスを取得する。
*/
const getFBService = () => {
return OAuth2.createService(fitbit.serviceName)
.setAuthorizationBaseUrl(fitbit.setAuthorizationBaseUrl)
.setTokenUrl(fitbit.tokenUrl)
.setClientId(fitbit.clientId)
.setClientSecret(fitbit.clientSecret)
.setCallbackFunction(fitbit.callback)
.setPropertyStore(property)
.setScope(fitbit.scope)
.setTokenHeaders({'Authorization': 'Basic ' + Utilities.base64Encode(fitbit.clientId + ':' + fitbit.clientSecret)});
}
function fbAuthCallback(request) {
var service = getFBService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
const fbLogoutFromService = () => {
getFBService().reset();
console.log("Logged out successfully")
}
/**
* Fitbitへヘルスデータ(体重・体脂肪率)を登録する。
*/
const fbPostHealthData = (service, healthData) => {
healthData.data.map((elem) => {
switch(elem.tag){
case BODY_FAT:
fbPostFat(service,elem.date,elem.keydata);
break;
case BODY_WEIGHT:
fbPostWeight(service,elem.date,elem.keydata);
break;
default:
console.log("unkown tag. tag = " + elem.tag);
break;
}
});
}
function fbPostWeight(service, date, weight){
urlWeight = 'https://api.fitbit.com/1/user/-/body/log/weight.json';
var headers = {
'Authorization': 'Bearer ' + service.getAccessToken()
};
var payloadWeight = {
'weight' : weight,
'date' : dayjs.dayjs(date, "YYYYMMDDHHmm").format('YYYY-MM-DD'),
'time' : dayjs.dayjs(date, "YYYYMMDDHHmm").format('HH:mm:00')
};
var optionsWeight = {
'method' : 'POST',
'payload': payloadWeight,
'headers' : headers,
'muteHttpExceptions': true
};
var responseWeight = UrlFetchApp.fetch(urlWeight, optionsWeight);
if (responseWeight.getResponseCode = HTTP_STATUS_CODE_OK) {
console.log("Fitbit weight logs(%s) have been registered successfully", date);
console.log(responseWeight.getContentText());
} else {
console.log("Failed to register Fitbit weight logs(%s).", date);
console.log(responseWeight.getResponseCode());
console.log(responseWeight.getContentText());
}
}
function fbPostFat(service, date, fat){
urlFat = 'https://api.fitbit.com/1/user/-/body/log/fat.json';
var headers = {
'Authorization': 'Bearer ' + service.getAccessToken()
};
var payloadFat = {
'fat' : fat,
'date' : dayjs.dayjs(date, "YYYYMMDDHHmm").format('YYYY-MM-DD'),
'time' : dayjs.dayjs(date, "YYYYMMDDHHmm").format('HH:mm:00')
};
var optionsFat = {
'method' : 'POST',
'payload': payloadFat,
'headers' : headers,
'muteHttpExceptions': true
};
var responseFat = UrlFetchApp.fetch(urlFat, optionsFat);
if (responseFat.getResponseCode = HTTP_STATUS_CODE_OK) {
console.log("Fitbit fat logs(%s) have been registered successfully", date);
console.log(responseFat.getContentText());
} else {
console.log("Failed to register Fitbit fat logs(%s).", date);
console.log(responseFat.getResponseCode());
console.log(responseFat.getContentText());
}
}