我正在尝试运行此脚本:
require_once('autoload.php');
require_once('Client.php');
$client = new Google_Client();
$client->setClientId('my clientid');
$client->setClientSecret('my secret');
$client->setDeveloperKey('my developer key');
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$client->setRedirectUri(null);
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/analytics'));
$service = new Google_Service_Analytics($client);
// get accounts from analytics
$accounts = $service->management_accounts->listManagementAccounts();
但是,它给了我这个错误:
PHP 致命错误:未捕获的异常"Google_Service_Exception" 消息 '{"错误":{"错误":[{"域":"全局","原因":"必需","消息":"登录 必需","位置类型":"标头","位置":"授权"}],"代码":401,"消息":"登录 必需"}}' in /root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php:129 堆栈跟踪:
#0/root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php(88): Google_Http_REST::d ecodeHttpResponse(Object(GuzzleHttp''Psr7''Response), object(guzzleHttp''Psr7''Request), 'Google_Service_...') #1 [内部函数]: Google_Http_REST::d oExecute(Object(GuzzleHttp''Client), object(guzzleHttp''Psr7''Request), 'Google_Service_...') #2/root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Task/Runner.php(181): call_user_func_array(阵列,阵列) #3/root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php(57): Google_Task_Runner->run() #4/root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Client.php(758): Google_Http_REST::execute(Object(GuzzleH in /root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php 在第 129 行
为什么?
您需要一个有效的重定向 Uri 而不是 null。
https://developers.google.com/api-client-library/php/auth/web-app