7

我想使用更新的 v3.0 访问我们的 Google Analytics 帐户报告,但从我阅读的所有内容看来,为了获得用户必须登录的有效访问令牌。

我们希望直接访问我们自己的帐户报告,而不是根据客户的帐户访问客户的报告。我们如何在 PHP 中完成此操作,而无需将浏览器发送到 Google 登录页面?v3.0 没有直接的 API 身份验证吗?

编辑

这似乎是在没有最终用户交互的情况下访问 API 的唯一方法,他们称之为“服务器到服务器”:

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

编辑 2

好像做不到?;(

警告:目前很少有 Google API 支持服务帐户。以下 Google 开发者服务目前支持服务帐号:

  • 谷歌云存储
  • 谷歌预测 API
  • 谷歌网址缩短器
  • 谷歌 OAuth 2.0 授权服务器

编辑 3

毕竟似乎有一个解决方案,因为我登录一次,然后使用“刷新令牌”来继续获得访问权限,而无需额外的用户登录。

4

1 回答 1

1

我确实最终使用了刷新令牌,它们工作正常。我通过使用 google api 控制台获得了一个 oauth 令牌,然后将其保存。

然后我只是在每个请求之前这样做:

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiAnalyticsService.php';;

$client = new apiClient();
$client->setApplicationName('My Analytics');
$client->setClientId($this->client_id);
$client->setClientSecret($this->client_secret);
$client->setDeveloperKey($this->api_key);

$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

$client->refreshToken($this->refresh_token);

$this->service = new apiAnalyticsService($client);
于 2012-12-01T03:08:05.607 回答