2016-03-04 69 views
-1

我試圖運行此腳本:誤差與谷歌的API-PHP認證介紹

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 Fatal error: Uncaught exception 'Google_Service_Exception' with message '{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}' in /root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php:129 Stack trace:
#0 /root/google-api/google-api-php-client-2.0.0-RC5/src/Google/Http/REST.php(88): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(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(Array, 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 on line 129

爲什麼?

+0

您是否試過閱讀任何文檔和/或調試? – KDOT

回答