通过PHP SDK向Google Fit发送重量

时间:2019-03-17 13:18:11

标签: php google-fit-sdk

我正尝试通过PHP向Google Fit API提交权重,并且已经读完Submit weight information / distance through the Google Fit REST API了。

我已请求具有范围的Auth令牌

https://www.googleapis.com/auth/fitness.body.write

我还创建了一个数据源,如果我使用POSTMAN进行Get on

https://www.googleapis.com/fitness/v1/users/me/dataSources

我找回了预期的详细信息

{
        "dataStreamId": "derived:com.google.weight:REDACTED",
        "dataStreamName": "Mediasana BS444",
        "type": "derived",
        "dataType": {
            "name": "com.google.weight",
            "field": [
                {
                    "name": "weight",
                    "format": "floatPoint"
                }
            ]
        },
        "device": {
            "uid": "REDACTED_MAC",
            "type": "scale",
            "version": "1",
            "model": "BS444",
            "manufacturer": "Mediasana"
        },
        "application": {
            "version": "1",
            "detailsUrl": "http://example.com",
            "name": "PHPBathroomScales"
        },
        "dataQualityStandard": []
    },

所以一切看起来都很不错,使用PHP SDK生成请求,我可以看到它最终会调用

https://www.googleapis.com/fitness/v1/users/me/dataSources/REDACTED/datasets/1535085014438000000-1535085014438000000

有效载荷为

{“ dataSourceId”:“已编辑”,“ maxEndTimeNs”:1535085014438000000,“ minStartTimeNs”:1535085014438000000,“ point”:{“ dataTypeName”:“ com.google.weight”,“ originDataSourceId”:“”,“ endTimeNanos “:1535085014438000000,” startTimeNanos“:1535085014438000000,” value“:{” fpVal“:88.8}}}

(与上述线程中的@ james-cole的示例大致匹配

并给出

的JSON响应
{
 "minStartTimeNs": "1535085014438000000",
 "maxEndTimeNs": "1535085014438000000",
 "dataSourceId": "REDACTED",
 "point": []
}

根据文档“如果成功,此方法将在响应正文中返回Users.dataSources.datasets资源”。看起来好像已经成功了,但是,要点空白表示不理想,随后的查询表明未添加任何权重。

有什么想法吗?

    $client = new Google_Client();
    $client->setApplicationName($app_name);
    $client->setScopes(implode(' ', array(Google_Service_Fitness::FITNESS_BODY_WRITE)));
    $client->setAuthConfig($client_secret);
    $client->setAccessType('offline');
    // Read and Update Access Code token removed for brevity
    return $client;


$dataStreamId = "REDACTED";
addWeight($fitness_service,$dataStreamId,new DateTime("2019-03-17 11:02:03",new DateTimeZone("UTC")),88.8);

function addWeight(&$service,$dataStreamId,$time,$value)
    {
        $time = intval($time->format("U")*1000000000);
        $minmax =  "{$time}-{$time}";
        $pointValue = new Google_Service_Fitness_Value();
        $pointValue->setFpVal(floatval($value));
        $pointPatch = new Google_Service_Fitness_DataPoint();
        $pointPatch->setDataTypeName("com.google.weight");
        $pointPatch->setEndTimeNanos($time);
        $pointPatch->setStartTimeNanos($time);
        $pointPatch->setValue($pointValue);
        $patchBody = new Google_Service_Fitness_Dataset();
        $patchBody->setDataSourceId($dataStreamId);
        $patchBody->setMaxEndTimeNs($time);
        $patchBody->setMinStartTimeNs($time);
        $patchBody->setPoint($pointPatch);
        $patchResult =  $service->users_dataSources_datasets->patch("me",$dataStreamId, $minmax, $patchBody);
}

0 个答案:

没有答案