目前手機上安裝Google Fit:活動追蹤功能
https://play.google.com/store/apps/details?id=com.google.android.apps.fitness&hl=zh_TW&gl=US
目前透過PHP與 google 授權,可取得使用者資料了,
<?php
$client = new Google_Client();
$client->addScope(['https://www.googleapis.com/auth/fitness.activity.read']);
$login_url = $client->createAuthUrl();
...略
$token = $client->getAccessToken(); // 取得 Token
$oauth = new Google_Service_Oauth2($client);
$profile = $oauth->userinfo->get();
$uid = $profile->id;
...略
可以回傳Token跟取得使用者ID,
請問要如何打API,以便可以取回使用者的步數資料呢?
https://developers.google.com/fit/datatypes/activity#rest_1
https://developers.google.com/fit/rest/v1/reference/users/dataSources/get
$url = "https://www.googleapis.com/fitness/v1/users/{{userId}}/dataSources/{{dataSourceId}}";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token . ',"Content-Type: application/json; charset=utf-8"'));
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'GET');
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
根據URL,默認情況下 $userId = "me", 接著你需要提供$dataSourceId。
根據https://developers.google.com/fit/rest/v1/reference/users/dataSources/list ,你可以試試GET https://www.googleapis.com/fitness/v1/users/me/dataSources
獲取$dataSourceIds信息
根據https://stackoverflow.com/questions/64956764/what-data-source-id-to-use-for-google-fit-rest-heart-rate-query ,你也可以試試"dataSourceId=derived:com.google.heart_rate.bpm:com.google.android.gms:resting_heart_rate<-merge_heart_rate_bpm"
最後,我建議你按照教程跑一次 > https://developers.google.com/fit/rest