首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用“同意屏幕”进行身份验证的获取访问令牌

不使用“同意屏幕”进行身份验证的获取访问令牌
EN

Stack Overflow用户
提问于 2014-02-11 17:05:57
回答 1查看 5.4K关注 0票数 3

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

一切都很好。我得到“同意屏幕”,我给予“许可”,我被重定向到我的网站,所有的信息。

但是,我想稍微改变一下。我想获得谷歌分析的所有信息,而不使用“同意屏幕”,即只使用谷歌分析代码(UA-XXXXXXXX X)或任何其他方式。

有什么帮助吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-12 09:39:42

为了使用Google,您需要授权(访问数据的权限)。因为您只想看到自己的数据,我建议您查看一个服务帐户。通过在Google控制台中设置一个服务帐户,它将允许您访问自己的数据,而无需随时登录和修改代码。

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

该示例项目是为PredictionService而不是谷歌分析服务提供的。你需要稍微编辑一下。

代码语言:javascript
复制
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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21708305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档