GoogleFit Google_Service_Exception:401登录要求

问题描述 投票:0回答:1

[尝试通过google fit API从google fit数据存储区获取以前日期的注册用户的步骤,当我手动运行该解决方案时效果很好,但是只要将其插入调度程序中以自动运行就会抛出该错误一个错误。这是我的代码:

public static function fetch(){
            $client = Helper::getProvider();
            #$client->setScopes('https://www.googleapis.com/auth/fitness.location.read https://www.googleapis.com/auth/fitness.activity.read');
            $client->addScope(\Google_Service_Fitness::FITNESS_ACTIVITY_READ);
            $service = new \Google_Service_Fitness($client);
            #$client->addScope(\Google_Service_Fitness::FITNESS_ACTIVITY_READ);
            $service = new \Google_Service_Fitness($client);
            // Same code as yours
            $dataSources = $service->users_dataSources;
            $dataSets = $service->users_dataSources_datasets;
            $listDataSources = $dataSources->listUsersDataSources("me");
            $timezone = "GMT+0100";
            $today = date("Y-m-d");
            $endTime = strtotime($today .' 00:00:00 '.$timezone);
            $startTime = strtotime('-1 day', $endTime);
            $step_count = 0;

            while($listDataSources->valid()) {
                $dataSourceItem = $listDataSources->next();
                if ($dataSourceItem['dataType']['name'] == "com.google.step_count.delta") {
                    $dataStreamId = $dataSourceItem['dataStreamId'];
                    $listDatasets = $dataSets->get("me", $dataStreamId, $startTime.'000000000'.'-'.$endTime.'000000000');

                    while($listDatasets->valid()) {
                        $dataSet = $listDatasets->next();
                        $dataSetValues = $dataSet['value'];

                        if ($dataSetValues && is_array($dataSetValues)) {
                            foreach($dataSetValues as $dataSetValue) {
                                $step_count += $dataSetValue['intVal'];
                            }
                        }
                    }
                }
            }
        return $step_count;
  }

public static function getProvider(){
        $client = new \Google_Client();
        $client->setApplicationName('google-fit');
        $client->setAccessType('offline');
        $client->setApprovalPrompt("auto");
        $client->setClientId('XX.apps.googleusercontent.com');
        $client->setClientSecret('XYV');
        return $client;
    }

我尝试运行此命令时遇到的错误是:

Google_Service_Exception: {
"error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
}
}
in ........./vendor/google/apiclient/src/Google/Http/REST.php:118
php laravel google-oauth google-api-php-client google-fit
1个回答
0
投票

因此,我修复了该错误(请不要否决该问题,此处留作记录)。我发现我跳过了没有在客户端变量上设置访问令牌($client->setAccessToken())的步骤。现在这是我所做的:

© www.soinside.com 2019 - 2024. All rights reserved.