【问题标题】:Google Analytics API - get Access Token without authenticate with the "consent screen"Google Analytics API - 无需通过“同意屏幕”进行身份验证即可获取访问令牌
【发布时间】:2014-03-09 14:52:43
【问题描述】:

我正在开发一个使用 Google Analytics API 的网站。我按照本教程进行操作-> https://developers.google.com/analytics/solutions/articles/hello-analytics-api?hl=pt-PT

一切正常。我得到“同意屏幕”,我给予“许可”,然后我被重定向到我的网站,包含所有信息。

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

有什么帮助吗?

谢谢

【问题讨论】:

  • 这就是 Oauth2 协议的工作方式。但是,您可以创建一个私钥并通过加载它,您也许可以跳过该身份验证步骤。

标签: php google-analytics google-analytics-api access-token


【解决方案1】:

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


这里是一个如何在phpServiceAccount中使用服务帐号的例子

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

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,您可以在其余通话中使用它。

【讨论】:

  • 感谢您的评论。我会检查这种可能性,然后给出反馈。 :)
猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2012-02-12
  • 2017-04-29
相关资源
最近更新 更多