我正在尝试将旧的 google api 迁移到新的 api,这样我就可以获取 google 分析数据。我正在尝试与this示例,但它会引发此错误
致命错误:找不到类“Google_Auth_AssertionCredentials”
示例.php
这就是我正在尝试的方式:
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 = '[email protected]';
$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
在我下载的图书馆中here,并且只有一个结果:upagrading.md
Google_Auth_AssertionCredentials removed use Google_Client::setAuthConfig instead
,
但我应该如何在构造函数中使用它呢?
I tred
$googleAssertionCredentials = new Google_Client::setAuthConfig(
$serviceAccountName,
$scopes
);
由于内部服务器错误,
知道我在这里缺少什么吗?
看起来你混合了新旧(谷歌 PHP API 客户端 2.0) 句法。消息“使用 Google_Client::setAuthConfig 代替”旨在指示要使用的方法,但并不是应该静态调用它。
它应该看起来像这样:
$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('[email protected]');
// set the authorization configuration using the 2.0 style
$client->setAuthConfig(array(
'type' => 'service_account',
'client_email' => '[email protected]',
'client_id' => '395545742105.apps.googleusercontent.com',
'private_key' => 'yourkey'
));
$analyticsService = new Google_Service_Analytics($client);
此语法适用于我撰写本文时的当前版本,即2.0.0-RC2
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)