2

我正在尝试从 Analytics 和 Youtube Analytics 中检索信息,但收效甚微。我正在使用 PHP API。

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

不幸的是,我不得不使用 oAuth2 协议,因为我还必须从 Youtube Analytics 中检索数据,并且它需要使用 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_Service_Exception' 带有消息 '{"error":{"errors":[{"domain":"global","re​​ason":"required","message":"Login Required","locationType ":"header","location":"Authorization"}],"code":401,"message":"需要登录"}}'

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

我错过了什么?

非常感谢您阅读并回答!

4

1 回答 1

1

错误 401 表示

Invalid Credentials 表示身份验证令牌无效或已过期。

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

资源

于 2016-10-17T08:56:45.357 回答