验证谷歌分析PHP API
我已经尝试过官方教程,但是它不起作用 我收到这个错误: “PHP致命错误:未找到类‘Google_Auth_AssertionCredentials’” 我的代码:验证谷歌分析PHP API,php,google-api,google-oauth,google-analytics-api,google-api-php-client,Php,Google Api,Google Oauth,Google Analytics Api,Google Api Php Client,我已经尝试过官方教程,但是它不起作用 我收到这个错误: “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 co
// 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主分支后,我现在得到以下错误
未捕获异常“Google\u服务\u异常”,并显示消息“错误”
致电GET googleapis.com/analytics/v3/management/accounts:(403)
用户没有任何Google Analytics帐户。”
对于您的问题,您需要在php“include_path”中添加库的基本目录 尝试将这行代码放在require_之前一次
set_include_path( get_include_path() . PATH_SEPARATOR . 'vendor/google/src' );
问题1:
如果您不使用composer,请确保下载了v1主分支—— 问题2: (403)用户没有任何Google Analytics帐户
默认情况下,您使用的是服务帐户。服务帐户无权访问任何google analytics帐户。您需要从Google开发者控制台获取服务帐户电子邮件地址,并将其添加为帐户级别的用户。它必须是Google Analytics网站管理部分下的帐户级别 我在vendor/google/中没有src,但我尝试了设置_include_path(get_include_path().path_SEPARATOR.'vendor/google/auth/src');如果您不使用composer,请确保您下载了v1 master branch thx,我已经尝试了此版本,但现在我收到了此错误未捕获的异常“Google_Service_exception”,并显示消息“error calling get:(403)User没有任何Google Analytics帐户”。请检查我刚才添加的答案。欢迎使用stack,我对您的问题进行了一些编辑,向您展示了如何设置问题格式,以便更好地阅读您的问题可能会帮助到的未来用户。thx获取帮助。你是说@developer.gserviceaccount.com,我怎么能得到它?它是你和你的.p12文件一起创建的。在谷歌开发者控制台上。在教程的这一部分中