致命错误:找不到类"Google_Auth_AssertionCredentials" [英] Fatal error: Class 'Google_Auth_AssertionCredentials' not found
问题描述
我正在尝试将旧的Google api迁移到新的,因此我可以获取Google Analytics(分析)数据.我正在尝试使用此例如,但会引发此错误
I'm trying to migrate the old google api to the new one, so I can get the google analytics data. I'm trying with this example, but it fires this error
致命错误:找不到"Google_Auth_AssertionCredentials"类 example.php
Fatal error: Class 'Google_Auth_AssertionCredentials' not found in example.php
这就是我正在尝试的方式:
This is how I'm trying:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'google-api-php-client/vendor/autoload.php';
//$p12FilePath = '/path/to/key.p12';
$serviceClientId = '395545742105.apps.googleusercontent.com';
$serviceAccountName = '395545742105@developer.gserviceaccount.com';
$scopes = array(
'https://www.googleapis.com/auth/analytics.readonly'
);
$googleAssertionCredentials = new Google_Auth_AssertionCredentials(
$serviceAccountName,
$scopes
); // <- Fatal error here
$client = new Google_Client();
$client->setAssertionCredentials($googleAssertionCredentials);
$client->setClientId($serviceClientId);
$client->setApplicationName("Project");
$analytics = new Google_Service_Analytics($client);
我确实在我从Google_Auth_AssertionCredentials >这里,只有一个结果:upagrading.md
And I did run a search for Google_Auth_AssertionCredentials
in the library wich I download from here, and Just one result: upagrading.md
Google_Auth_AssertionCredentials removed use Google_Client::setAuthConfig instead
,
但是我应该如何在建设者中使用它?
But how should I use it in a contructor?
我尝试过
$googleAssertionCredentials = new Google_Client::setAuthConfig(
$serviceAccountName,
$scopes
);
内部服务器错误,
知道我在这里缺少什么吗?
Any idea what I'm missing here?
推荐答案
It looks like you have a mixture of the old and new (Google PHP API Client 2.0) syntax. The message "use Google_Client::setAuthConfig instead" is meant to indicate the method to use, but not that it should be called statically.
它应该看起来像这样:
$client = new Google_Client();
// set the scope(s) that will be used
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
// this is needed only if you need to perform
// domain-wide admin actions, and this must be
// an admin account on the domain; it is not
// necessary in your example but provided for others
$client->setSubject('youradmin@example.com');
// set the authorization configuration using the 2.0 style
$client->setAuthConfig(array(
'type' => 'service_account',
'client_email' => '395545742105@developer.gserviceaccount.com',
'client_id' => '395545742105.apps.googleusercontent.com',
'private_key' => 'yourkey'
));
$analyticsService = new Google_Service_Analytics($client);
在撰写本文时,该语法对我当前的版本适用,即2.0.0-RC2
.
This syntax works for me with the current build as of this writing, which is 2.0.0-RC2
.
这篇关于致命错误:找不到类"Google_Auth_AssertionCredentials"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!