[英]Auth Google Analytics PHP API
我已經嘗試過官方的HelloAnalytics教程,但它不起作用。
我收到這個錯誤:
“PHP致命錯誤:未找到類'Google_Auth_AssertionCredentials'
我的代碼:
// 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分支后,我現在收到以下錯誤
未捕獲的異常“Google_Service_Exception”,並顯示消息“錯誤呼叫GET googleapis.com/analytics/v3/management/accounts :( 403)用戶沒有任何Google Analytics帳戶。”
對於您的問題,您需要在php'include_path'中添加庫的基目錄。
嘗試在require_once之前放置這行代碼
set_include_path( get_include_path() . PATH_SEPARATOR . 'vendor/google/src' );
問題1:
如果你沒有使用作曲家,請確保你下載了v1-master分支 - 鏈接
問題2:
(403)用戶沒有任何Google Analytics帳戶。
您默認使用服務帳戶,服務帳戶無權訪問任何Google Analytics分析帳戶。 您需要從Google Developers控制台獲取服務帳戶電子郵件地址,並以ACCOUNT級別的用戶身份將其添加為管理部分下Google Analytics網站中的ACCOUNT級別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.