4

我正在尝试根据此文档使用 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在控制台项目中启用并添加了 scope https://www.google.com/m8/feeds

检索到的 ID 是否与用户的联系人 ID不同,或者有没有办法获取用户的联系人 ID或我的代码中有什么问题? 任何帮助或意见表示赞赏。

4

1 回答 1

0

我正在使用带有类似代码的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吗?

于 2014-03-20T08:28:51.643 回答