我想通过后台 cron 作业将谷歌分析中的数据提取到自己的数据库中,而无需用户每次都进行身份验证。
我知道如何通过用户交互获取 Google Analytics OAuth 访问令牌 https://stackoverflow.com/questions/26399357/where-to-optain-google-analytics-api-access-token正如我之前所问的。使用 OAuth 不起作用,因为它需要用户交互。根据谷歌分析 API 参考 https://developers.google.com/analytics/devguides/reporting/core/v3/referenceOAuth 以及访问令牌可用于在每个会话的基础上访问一次页面的统计信息。不过,我正在寻找一种持久的方法来在后台服务中实现这一点。
如何在没有用户身份验证的情况下访问谷歌分析或获取不过期的访问令牌?
假设相关帐户是您自己的,您可以服务帐户 https://developers.google.com/accounts/docs/OAuth2ServiceAccount。确保您在帐户级别向服务帐户电子邮件地址授予 Google 分析帐户的读取权限,这样它将能够读取您的数据。
<?php
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
/************************************************
The following 3 values an befound in the setting
for the application you created on Google
Developers console.
The Key file should be placed in a location
that is not accessable from the web. outside of
web root.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/analytics.readonly";
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Analytics($client);
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
//calulating start date
$date = new DateTime(date("Y-m-d"));
$date->sub(new DateInterval('P10D'));
//Adding Dimensions
$params = array('dimensions' => 'ga:userType');
// requesting the data
$data = $service->data_ga->get("ga:78110423", $date->format('Y-m-d'), date("Y-m-d"), "ga:users,ga:sessions", $params );
?><html>
<?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "\n";?>
<table>
<tr>
<?php
//Printing column headers
foreach($data->getColumnHeaders() as $header){
print "<td>".$header['name']."</td>";
}
?>
</tr>
<?php
//printing each row.
foreach ($data->getRows() as $row) {
print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";
}
//printing the total number of rows
?>
<tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>
</table>
</html>
<?php
?>
从教程中摘取的代码使用 PHP 的 Google 服务帐户 http://www.daimto.com/google_service_account_php/
如果这不是您的帐户,那么您可以使用正常的 Oauth2 方法要求用户对您进行身份验证一次,然后使用刷新令牌您将能够访问数据。使用您上一个问题中的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)