问题
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
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);
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?
I tred
$googleAssertionCredentials = new Google_Client::setAuthConfig(
$serviceAccountName,
$scopes
);
With internal server error,
Any idea what I'm missing here?
回答1:
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.
It should look like this:
$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);
This syntax works for me with the current build as of this writing, which is 2.0.0-RC2
.
回答2:
This solution work for me:
The composer setup section in https://github.com/google/google-api-php-client/blob/master/README.md could mention both versions, something like:
composer require google/apiclient:^2.0.0@RC Note the documentation at developers.google.com refers to V1 of this library. If you want to use the older version, instead use: composer require google/apiclient:1.*
https://github.com/google/google-api-php-client/issues/748
来源:https://stackoverflow.com/questions/34130068/fatal-error-class-google-auth-assertioncredentials-not-found