授权 Google 分析 PHP API
Auth Google Analytics PHP API
我试过官方的HelloAnalytics教程,但是它不起作用。
我收到此错误:
"PHP Fatal error: Class 'Google_Auth_AssertionCredentials' not found"
我的代码:
// Creates and returns the Analytics service object.
// Load the Google API PHP Client Library.
require_once 'vendor/autoload.php';
// Use the developers console and replace the values with your
// service account email, and relative location of your key file.
$service_account_email = 'xxxxxxxxxxx@gmail.com';
$key_file_location = 'key_anyl.p12';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_email,
array(Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
更新:
添加建议的 V1-master 分支后,我现在收到以下错误
Uncaught exception 'Google_Service_Exception' with message 'Error
calling GET googleapis.com/analytics/v3/management/accounts: (403)
User does not have any Google Analytics account.'
对于您的问题,您需要在 php 'include_path'.
中添加库的基本目录
尝试将这行代码放在 require_once
之前
set_include_path( get_include_path() . PATH_SEPARATOR . 'vendor/google/src' );
问题 1:
如果您不使用 composer,请确保您下载了 v1-master 分支 – Link
问题 2:
(403) User does not have any Google Analytics account.
默认情况下,您使用的是服务帐户,服务帐户无权访问任何 google 分析帐户。您需要从 Google 开发人员控制台获取服务帐户电子邮件地址,并将其添加为帐户级别的用户,它必须是 Google 分析网站管理部分下的帐户级别。
我试过官方的HelloAnalytics教程,但是它不起作用。
我收到此错误:
"PHP Fatal error: Class 'Google_Auth_AssertionCredentials' not found"
我的代码:
// Creates and returns the Analytics service object.
// Load the Google API PHP Client Library.
require_once 'vendor/autoload.php';
// Use the developers console and replace the values with your
// service account email, and relative location of your key file.
$service_account_email = 'xxxxxxxxxxx@gmail.com';
$key_file_location = 'key_anyl.p12';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_email,
array(Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
更新:
添加建议的 V1-master 分支后,我现在收到以下错误
Uncaught exception 'Google_Service_Exception' with message 'Error calling GET googleapis.com/analytics/v3/management/accounts: (403) User does not have any Google Analytics account.'
对于您的问题,您需要在 php 'include_path'.
中添加库的基本目录尝试将这行代码放在 require_once
之前set_include_path( get_include_path() . PATH_SEPARATOR . 'vendor/google/src' );
问题 1:
如果您不使用 composer,请确保您下载了 v1-master 分支 – Link
问题 2:
(403) User does not have any Google Analytics account.
默认情况下,您使用的是服务帐户,服务帐户无权访问任何 google 分析帐户。您需要从 Google 开发人员控制台获取服务帐户电子邮件地址,并将其添加为帐户级别的用户,它必须是 Google 分析网站管理部分下的帐户级别。