通过PHP API将步骤发送到Google Fit

时间:2018-06-24 12:28:59

标签: google-api google-api-php-client google-fit google-fit-sdk

我无法通过PHP API向Google Fit提交数据。 经过大量的修改,我能够成功使用OAuth 2.0 Playground通过Fitness API添加“步数”数据。

https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:<redacted>/datasets/1529492401000000000-1529514001000000000上使用PATCH, 我添加了一个

{
  "dataSourceId":
      "derived:com.google.step_count.delta:<redacted>",
  "maxEndTimeNs": 1529514001000000000,
  "minStartTimeNs": 1529492401000000000,
  "point": [
    {
      "dataTypeName": "com.google.step_count.delta",
      "endTimeNanos": 1529514001000000000,
      "originDataSourceId": "",
      "startTimeNanos": 1529492401000000000,
      "value": [
        {
          "intVal": 20
        }
      ]
    }
  ]
}

添加20个步骤就可以了。

但是,过渡到PHP是不可能的。我可以进行身份​​验证,但是当我尝试发送步骤时,我在Fit数据中看不到任何添加的步骤。我也没有看到任何错误。

以下是最相关的代码:

require_once 'google-api-php-client-2.2.1/vendor/autoload.php';

$client_id = '<redacted>.apps.googleusercontent.com';
$client_secret = '<redacted>';
$redirect_uri = '<redacted>';

//This template is nothing but some HTML. You can find it on github Google API example. 
include_once "templates/base.php";

//Start your session.
session_start();

$client = new Google_Client();
$client->setApplicationName('google-fit');
$client->setAccessType('online');
$client->setApprovalPrompt("auto");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

$client->addScope(Google_Service_Fitness::FITNESS_ACTIVITY_WRITE);
$service = new Google_Service_Fitness($client);

/************************************************
If we're logging out we just need to clear our
local access token in this case
 ************************************************/
if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
}
/************************************************
If we have a code back from the OAuth 2.0 flow,
we need to exchange that with the authenticate()
function. We store the resultant access token
bundle in the session, and redirect to ourself.
 ************************************************/
if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    echo "EXCHANGE";
}
/************************************************
If we have an access token, we can make
requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);

    $minmax = "1529492401000000000-1529514001000000000";

    $pointValue = new Google_Service_Fitness_Value();
    $pointValue->setIntVal(20);

    $pointPatch = new Google_Service_Fitness_DataPoint();
    $pointPatch->setDataTypeName("com.google.step_count.delta");
    $pointPatch->setEndTimeNanos(1529514001000000000);
    $pointPatch->setStartTimeNanos(1529492401000000000);
    $pointPatch->setValue($pointValue);

    $patchBody = new Google_Service_Fitness_Dataset();
    $patchBody->setDataSourceId("derived:com.google.step_count.delta:<redacted>");
    $patchBody->setMaxEndTimeNs(1529514001000000000);
    $patchBody->setMinStartTimeNs(1529492401000000000);
    $patchBody->setPoint($pointPatch);


    $patchResult =  $service->users_dataSources_datasets->patch("me","derived:com.google.step_count.delta:<redacted", $minmax, $patchBody);

(该代码是对此处How to get steps count with Google Fit api in php ?答案的修改)

有什么想法吗?

0 个答案:

没有答案