如何修复:Google Fitness REST API -需要至少一个聚合方式[PHP]

2nbm6dog  于 2023-03-22  发布在  PHP
关注(0)|答案(1)|浏览(108)

它可以测试和显示正确的数据在这里(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代码,但它总是返回“至少需要一个aggregateby”。
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"
  }
}

是否有人有变通方案,示例是什么?

gopyfrb3

gopyfrb31#

我不是PHPMaven,但我相信你需要像这样编码你的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_encode({...})这样不使用单引号 Package JSON

相关问题