2017-01-08 47 views
2

我使用谷歌的登錄並使用此代碼爲AUTH如何讓谷歌登錄後接觸

$client = new Google_Client(); 
$client->setApplicationName("Google OAuth Login Example"); 
$client->setClientId($client_id); 
$client->setClientSecret($client_secret); 
$client->setRedirectUri($redirect_uri); 
$client->setDeveloperKey($simple_api_key); 
$client->setAccessType('offline'); 
$client->setScopes(array('https://www.google.com/m8/feeds/',"https://www.googleapis.com/auth/userinfo.email")); 
$objOAuthService = new Google_Service_Oauth2($client); 

現在,這裏從客戶獲得訪問令牌,並使用此訪問令牌從獲取用戶的聯繫人

$accessToken = client->getAccessToken(); 
    if ($client->getAccessToken()) { 
    $userData = $objOAuthService->userinfo->get(); 
    $_SESSION['access_token'] = $client->getAccessToken(); 

} 

現在我想登錄用戶與用戶聯繫該API和訪問令牌我從谷歌用戶的身份驗證過程中得到了

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=' . $max_results . '&oauth_token=' . $accessToken; 
$xmlresponse = curl_file_get_contents($url); 
echo 'contact result='.$xmlresponse; 

但我有401 http狀態,無法獲取用戶聯繫人。請幫助我如何使用訪問令牌登錄用戶聯繫電子郵件,這是我在授權過程中得到的。

回答

0

正確的參數名稱是access_token而不是oauth_token。

試試這個:

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=' . $max_results . '&access_token=' . $accessToken; 
$xmlresponse = curl_file_get_contents($url); 
echo 'contact result='.$xmlresponse; 

注:谷歌聯繫人API是一個古老的GData API,您將無法使用谷歌的PHP客戶端庫來訪問它。您應該查看Google People API,我發現返回的數據與Google聯繫人中的數據類似。