首页 文章

没有客户端登录的Google Analytics核心报告API 3.0版

提问于
浏览
7

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

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

EDIT

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

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

EDIT 2

看起来无法完成? (

警告:目前很少有Google API支持服务帐户 . 以下Google开发人员服务目前支持服务帐户:Google Cloud 端存储Google预测API Google URL缩短器Google OAuth 2.0授权服务器

EDIT 3

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

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);
    

相关问题