可以在这里进行测试并显示正确的数据( https://developers.google.com/oauthplayground/)
POST /fitness/v1/users/me/dataset:aggregate HTTP/1.1
Host: fitness.googleapis.com
Content-length: 252
Content-type: application/json
Authorization: Bearer ya29.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{
"aggregateBy": [{
"dataSourceId":
"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1659283200000,
"endTimeMillis": 1659628799000
}
请求/响应:
{
"bucket": [
{
"startTimeMillis": "1659283200000",
"endTimeMillis": "1659369600000",
"dataset": [
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": []
}
]
},
{
"startTimeMillis": "1659369600000",
"endTimeMillis": "1659456000000",
"dataset": [
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1659506670385374313",
"originDataSourceId": "raw:com.google.step_count.cumulative:nokia:TT-B215C:dfcqfe387:nokia Step Counter",
"endTimeNanos": "1659528570452802007",
"value": [
{
"mapVal": [],
"intVal": 222222
}
],
"dataTypeName": "com.google.step_count.delta"
}
]
}
]
},
{
"startTimeMillis": "1659456000000",
"endTimeMillis": "1659542400000",
"dataset": [
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1659506670385374313",
"originDataSourceId": "raw:com.google.step_count.cumulative:nokia:TT-B215C:dfcqfe387:nokia Step Counter",
"endTimeNanos": "1659528570452802007",
"value": [
{
"mapVal": [],
"intVal": 10005
}
],
"dataTypeName": "com.google.step_count.delta"
}
]
}
]
},
{
"startTimeMillis": "1659542400000",
"endTimeMillis": "1659628799000",
"dataset": [
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1659549150591339815",
"originDataSourceId": "raw:com.google.step_count.cumulative:nokia:TT-B215C:dfcqfe387:nokia Step Counter",
"endTimeNanos": "1659595140887199335",
"value": [
{
"mapVal": [],
"intVal": 50005
}
],
"dataTypeName": "com.google.step_count.delta"
}
]
}
]
}
]
}
我尝试将方法转换为 PHP 代码, 但它总是返回“至少需要一个聚合”。
PHP 代码在这里
<?php
$token_type = 'Bearer';
$access_token = 'ya29.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // must change to yours token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://fitness.googleapis.com/fitness/v1/users/me/dataset:aggregate',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => array('Authorization: ' . $token_type .' ' . $access_token),
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"aggregateBy": [{
"dataSourceId":
"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1659283200000,
"endTimeMillis": 1659628799000
}',
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
错误请求/响应:
{
"error": {
"code": 400,
"message": "Require at least one aggregateby",
"errors": [
{
"message": "Require at least one aggregateby",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}
有人有解决方法吗? 举个什么例子?
我不是 PHP 专家,但我相信你需要像这样编码你的 JSON 主体......
CURLOPT_POSTFIELDS => json_encode('{
"aggregateBy": [{
"dataSourceId":
"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1659283200000,
"endTimeMillis": 1659628799000
}'),
不确定 JSON 是否需要这样引用
json_encode('{...}')
。如果这不起作用,请尝试不使用单引号将 json 括起来,如下所示 json_encode({...})