我a small card game for Google+需要访客的姓名,头像,性别和城市。
它适用于我自己,但在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并且我正在请求非常基本的用户信息,正如我所写的那样 - 它适用于我和我妻子的帐户。
答案 0 :(得分:3)
你可以在try / catch块中包装$ me = $ plus-&gt; people-&gt; get('me')。
当用户尚未注册Google+时,plus / v1 / people / me API会返回404 Not Found,您可以通过以下方式查看此案例:
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();
}
答案 1 :(得分: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);
}
}