我正在尝试根据本文档使用Google Contacts API检索联系人的照片。使用的PHP客户库在这里。我成功地获得了代码和用户信息,但未能获取用户的照片数据。
我得到的响应是:
照片查询失败 - 无效联系人ID
。
这是我用来获取用户照片数据的回调函数的PHP代码:
$client = new Google_Client();
$client->setClientId(GOOGLE_APP_CLIENT_ID);
$client->setClientSecret(GOOGLE_APP_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_APP_CLIENT_REDIRECT_URI);
$client->addScope("openid profile email https://www.google.com/m8/feeds");
// Create a OAuth 2 service instance
$oauth2 = new Google_Service_Oauth2($client);
// Get the access token using code.
if (isset($_GET['code'])) {
// Authenticate the code
$client->authenticate($_GET['code']);
$_SESSION['access_code'] = $client->getAccessToken();
// Set the access token
$client->setAccessToken($_SESSION['access_token']);
$temp_obj = $client->verifyIdToken();
....
// Get the user's info
$guser_info = $oauth2->userinfo->get();
// Get the contact id
$contact_id = $guser_info['id'];
// Get the user email
$user_email = $guser_info['email'];
// Make an authenticated request to the photo
$req = new Google_Http_Request("https://www.google.com/m8/feeds/photos/media/default/$contact_id");
// For replacing above line with the following also got the same result.
//$req = new Google_Http_Request("https://www.google.com/m8/feeds/photos/media/$user_email/$contact_id");
// Make an authenticated request
$val = $client->getAuth()->authenticatedRequest($req);
print_r($val->getResponseBody());// Get "Photo query failed - invalid contact ID"
} ...
我在控制台项目中启用了Contacts API
并添加了范围https://www.google.com/m8/feeds
。
是检索到的 id 与用户的联系ID 不同,或者有一种方法可以获取用户的联系人ID 或我的代码中有问题?
任何帮助或评论都将不胜感激。
我正在使用带有类似代码的Hybridauth获取用户配置文件图像(Laravel样式):
<?php
//@see http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
public function getSocial($action = '')
{
// check URL segment
if ($action == 'auth') {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::to('login/social');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with provider
if($action != ''){
if(in_array($action,array('google'))){
$provider = $socialAuth->authenticate($action);
}else{
return Redirect::to('login');
}
}else{
return Redirect::to('login');
}
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return Redirect::to('login');
}
access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
echo "PhotoURL: {$userProfile->photoUrl}<br />";
die();
}
也许您的$guser_info
已经包含了照片的链接?您是否尝试过print_r
?