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

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

问题描述

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

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.

但是,我想稍微改变一下.我想获取 Google Analytics 的所有信息,而不使用同意屏幕",即仅使用 Google Analytics 代码 (UA-XXXXXXXX-X) 或任何其他方式.

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.

有什么帮助吗?

谢谢

推荐答案

为了使用 Google Analytics API,您需要授权(访问数据的权限).由于您只想查看自己的数据,我建议您查看服务帐户.通过在 Google apis 控制台中设置服务帐户,您可以访问自己的数据,而无需一直登录和验证代码.

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.

这里是如何在 php 中使用服务帐户的示例 ServiceAccount

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

该示例项目用于 PredictionService,而不是 google 分析服务.你需要稍微编辑一下.

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

现在您拥有了 $service,您可以将其用于其余的调用.

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

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

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