Php Google Analytics API-oAuth2身份验证失败

Php Google Analytics API-oAuth2身份验证失败,php,google-analytics-api,google-api-php-client,google-oauth,youtube-analytics-api,Php,Google Analytics Api,Google Api Php Client,Google Oauth,Youtube Analytics Api,我试图从分析和Youtube分析中检索信息,但收效甚微。我正在使用PHPAPI 我已尝试连接服务帐户,它工作正常 不幸的是,我不得不使用oAuth2协议,因为我还必须从Youtube分析中检索数据,并且需要使用oAuth2身份验证 以下是我正在使用的代码: require_once 'vendor/autoload.php'; // create the client and connect $client = new Google_Client(); $client->setAuthC

我试图从分析和Youtube分析中检索信息,但收效甚微。我正在使用PHPAPI

我已尝试连接服务帐户,它工作正常

不幸的是,我不得不使用oAuth2协议,因为我还必须从Youtube分析中检索数据,并且需要使用oAuth2身份验证

以下是我正在使用的代码:

require_once 'vendor/autoload.php';

// create the client and connect
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);

// send the query
$service = new Google_Service_Analytics($client);
$id = 'ga:XXXXXX';
$start_date = '2016-09-01';
$end_date = '2016-09-10';
$metrics = 'ga:visitors';
$data = $service->data_ga->get($id, $start_date, $end_date, $metrics);
以下是错误消息:

致命错误:未捕获的异常“Google\u服务\u异常”与 消息 {“error”:{“errors”:[{“domain”:“global”,“reason”:“required”,“message”:“Login” 必填项,“位置类型”:“标题”,“位置”:“授权”}],“代码”:401,“消息”:“登录” 必需的“}”

我已经试了几个小时来解决这个错误,但我找不到原因。我的第一个想法是setAuthConfig不会自动记录我的日志,但我在web上看到了许多使用完全相同方法的示例,包括在API文档中

我错过了什么

非常感谢您的阅读和回答

错误401表示

无效凭据表示身份验证令牌无效或已过期

您应该获得一个新的身份验证令牌