Google Analytics API - 无需通过“同意屏幕”验证即可获取访问令牌 [英] Google Analytics API - get Access Token without authenticate with the "consent screen"

查看:601
本文介绍了Google Analytics API - 无需通过“同意屏幕”验证即可获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Google AnalyticsAPI的网站。我遵循本教程 - > https://开发人员.google.com / analytics / solutions / articles / hello-analytics-api?hl = pt-PT



一切正常。我得到了同意屏幕,我给了权限,并将其重定向到了我的网站,并提供了所有信息。



但是,我想改变这些事情小。我希望获取Google Analytics的所有信息,而不使用同意屏幕,即仅使用Google Analytics代码(UA-XXXXXXXX-X)或任何其他方式。



任何帮助?

谢谢

解决方案

使用您需要授权的Google analytics API(访问数据的权限)。如果您只想查看自己的数据,我建议您查看服务帐户。通过在Google apis控制台中设置服务帐户,您可以访问自己的数据,无需登录并始终验证代码。




以下是如何在php ServiceAccount



该示例项目用于PredictionService而不是谷歌分析服务。你需要稍微修改它。

  require_once'../../src/Google/Client.php'; 
require_once'../../src/Google/Service/Analytics.php';

//设置您的客户端ID,服务帐户名称和私钥的路径。
//有关获取这些密钥的更多信息,请访问:
// https://developers.google.com/console/help/#service_accounts

const CLIENT_ID =' INSERT_YOUR_CLIENT_ID;
const SERVICE_ACCOUNT_NAME ='INSERT_YOUR_SERVICE_ACCOUNT_NAME';


//确保你的key.p12文件保存在一个安全的位置,而不是
//被其他人读取。

const KEY_FILE ='/super/secret/path/to/key.p12';


$ client = new Google_Client();
$ client-> setApplicationName(Google Analytics示例);


//以PKCS 12格式加载密钥(您需要在创建服务帐户时从
// Google API控制台下载


$ client-> setAssertionCredentials(新Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME(电子邮件),
数组('https://www.googleapis.com/auth/analytics.readonly')) ,
file_get_contents(KEY_FILE))
);


$ client-> setClientId(CLIENT_ID);
$ service = new Google_Service_Analytics($ client );

现在你有 $ service 您可以与其他通话一起使用。


I'm developing a website that uses Google Analytics API. I followed this tutorial -> https://developers.google.com/analytics/solutions/articles/hello-analytics-api?hl=pt-PT

and everything works fine. I get the "consent screen", I give "permissions" and I'm redirected to my website, with all information.

But, I want to change the things a little. I want to get all info of Google Analytics, without using the "consent screen", i.e. , using just the Google Analytics code (UA-XXXXXXXX-X) or any other way.

Any help?

Thanks

解决方案

In order to use the Google analytics API you need authorization (permission to access the data). Becouse you only want to see your own data I recommend that you look into a service account. By setting up a service account in Google apis console it will allow you to access your own data with out needing to login and autenticate the code all the time.


Here is an example of how to use a service account in php ServiceAccount

That sample project is for the PredictionService not the google analytics service. You need to edit it slightly.

require_once '../../src/Google/Client.php'; 
require_once '../../src/Google/Service/Analytics.php'; 

// Set your client id, service account name, and the path to your private key. 
// For more information about obtaining these keys, visit: 
// https://developers.google.com/console/help/#service_accounts

const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID'; 
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME'; 


// Make sure you keep your key.p12 file in a secure location, and isn't 
// readable by others.

const KEY_FILE = '/super/secret/path/to/key.p12'; 


$client = new Google_Client(); 
$client->setApplicationName("Google Analytics Sample"); 


// Load the key in PKCS 12 format (you need to download this from the 
// Google API Console when the service account was created. 


$client->setAssertionCredentials(new Google_AssertionCredentials( 
    SERVICE_ACCOUNT_NAME(Email), 
    array('https://www.googleapis.com/auth/analytics.readonly'), 
    file_get_contents(KEY_FILE)) 
); 


$client->setClientId(CLIENT_ID); 
$service = new Google_Service_Analytics($client);

Now you have $service that you can use with the rest of your calls.

这篇关于Google Analytics API - 无需通过“同意屏幕”验证即可获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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