2012-01-29 325 views
2

我有a small card game for Google+需要訪客的名字,頭像,性別和城市。未捕獲的異常'apiServiceException'消息'錯誤調用GET ....'

它適用於自己,但會在error_log我看到很多PHP-例外:

[28-Jan-2012 19:06:33] PHP Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling GET https://www.googleapis.com/plus/v1/people/me?alt=json&key=AIzaSyAgQl0UeNM553PfLnPmP0jTtcJ8ZIQ3q0g: (404) Not Found' in /var/www/html/preferans.de/google/google-api-php-client/src/io/apiREST.php:86 
Stack trace: 
#0 /var/www/html/preferans.de/google/google-api-php-client/src/io/apiREST.php(56): apiREST::decodeHttpResponse(Object(apiHttpRequest)) 
#1 /var/www/html/preferans.de/google/google-api-php-client/src/service/apiServiceResource.php(151): apiREST::execute(Object(apiServiceRequest)) 
#2 /var/www/html/preferans.de/google/google-api-php-client/src/contrib/apiPlusService.php(207): apiServiceResource->__call('get', Array) 
#3 /var/www/html/preferans.de/google/index.php(33): PeopleServiceResource->get('me') 
#4 {main} 
    thrown in /var/www/html/preferans.de/google/google-api-php-client/src/io/apiREST.php on line 86 

這裏是我的腳本:

<?php 

require_once('google-api-php-client/src/apiClient.php'); 
require_once('google-api-php-client/src/contrib/apiPlusService.php'); 

session_start(); 

$client = new apiClient(); 
$client->setApplicationName('Video-Preferans'); 
$client->setClientId('XXX.apps.googleusercontent.com'); 
$client->setClientSecret('XXX'); 
$client->setRedirectUri('http://preferans.de/google'); 
$client->setDeveloperKey('XXX'); 
$client->setScopes(array('https://www.googleapis.com/auth/plus.me')); 
$plus = new apiPlusService($client); 

if (isset($_REQUEST['logout'])) 
     unset($_SESSION['access_token']); 

if (isset($_GET['code'])) { 
     $client->authenticate(); 
     $_SESSION['access_token'] = $client->getAccessToken(); 
     header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 
} 

if (isset($_SESSION['access_token'])) 
     $client->setAccessToken($_SESSION['access_token']); 

if ($client->getAccessToken()) { 
     $me = $plus->people->get('me'); # XXX line 33 XXX 
     # the access token may have been updated lazily 
     $_SESSION['access_token'] = $client->getAccessToken(); 
} else { 
     printf(' 
<!doctype html> 
<html> 
<head> 
<body> 
<p><a href="%s">Play Preferans</a></p> 
</body> 
</html> 
', $client->createAuthUrl()); 
     exit(); 
} 

$viewer_id = $me['id']; 
list($first_name, $last_name) = explode(' ', $me['displayName']); 
$city  = $me['placesLived'][0]['value']; 
$female = ($me['gender'] == 'male' ? 0 : 1); 
$avatar = $me['image']['url']; 

....skipped some html code.... 

有誰請知道,爲什麼apiServiceException被拋出?

或者如何以及在哪裏至少抓住它,以便我可以更好地調試它?

我使用的是the latest Google+ SDK 0.4.8.3而且我正在請求非常基本的用戶信息,正如我寫的 - 它適用於我和我妻子的帳戶。

回答

3

你可以在try/catch塊中包裝$ me = $ plus-> people-> get('me')。

號/ V1 /人/我API返回404,當用戶還沒有註冊使用Google+沒有找到,你可以趕上這種情況下有以下:

try { 
    $me = $plus->people->get('me') 
} catch (apiServiceException $e) { 
    // Handle exception. You can also catch Exception here. 
    // You can also get the error code from $e->getCode(); 
} 
+0

上面,我得到一個403錯誤catch塊並沒有爲我工作 – dspacejs 2015-09-15 05:49:21

1

我有同樣的問題,唯一不同的是我正在嘗試使用Calendar API。我得到了與第一個完全相同的錯誤響應,嘗試了try/catch-block,它有效。如果我打印錯誤響應它說「403」,但我也得到我想要的JSON響應。你知道爲什麼嗎?

我的代碼:

if ($client->getAccessToken()) { 
    try{ 
     $activities = $plus->activities->listActivities('me', 'public'); 
     print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>'; 

     // The access token may have been updated. 
     $_SESSION['token'] = $client->getAccessToken(); 
    } catch (apiServiceException $e) { 
     // Handle exception. You can also catch Exception here. 
     // You can also get the error code from $e->getCode(); 
     echo $e->getCode(); 
     print_r($activities); 
    } 

} 
+0

當我嘗試做。那是什麼意思 ? – 2012-05-25 13:45:32

相關問題