2012-01-29 326 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

相关问题