Google Analytics核心报告API版本3.0,无需客户端登录 [英] Google Analytics Core Reporting API Version 3.0 without client login

查看:112
本文介绍了Google Analytics核心报告API版本3.0,无需客户端登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



编辑



似乎是在没有最终用户交互的情况下访问API的唯一方法,他们称之为Server to Server: .google.com / accounts / docs / OAuth2ServiceAccountrel =noreferrer> https://developers.google.com/accounts/docs/OAuth2ServiceAccount



编辑2



看起来不能完成? ;(b / b)
$ b


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




  • Google云端存储

  • Google预测API

  • Google URL Shortener

  • Google OAuth 2.0授权服务器




编辑3



毕竟,当我登录一次然后使用刷新令牌继续访问,无需额外的用户登录。 ,它们工作的很好,我使用google api控制台得到了一个oauth标记,然后保存了它。

然后我在每次请求前都这么做:

  require_once'google-api-php-client / src / apiClient.php'; 
require_once'google-a PI-PHP-客户/ 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);


I want to access our Google Analytics account reporting using the newer v3.0, but it seems from everything that I read that in order to get a valid access token the user must log in.

We want direct access to our own account reporting, and not access a client's depending on their account. How do we accomplish this in PHP without having to send the browser to a Google login page? Is there no straight API authentication for v3.0?

EDIT

This seems to be the only method of accessing the API without end-user interaction, which they call "Server to Server":

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

EDIT 2

Looks like it can't be done? ;(

Warning: Very few Google APIs currently support Service Accounts. Service accounts are currently supported by the following Google developer services:

  • Google Cloud Storage
  • Google Prediction API
  • Google URL Shortener
  • Google OAuth 2.0 Authorization Server

EDIT 3

There seems to be a solution after all, as I log in once and then use "Refresh Tokens" to keep gaining access without an additional user login.

解决方案

I did end up using the refresh tokens, they work fine. I got a oauth token by using the google api console, and then saved it.

Then I just do this before each request:

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

这篇关于Google Analytics核心报告API版本3.0,无需客户端登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆