2012-03-09 91 views
0

我正在努力獲取登錄的用戶詳細信息。它出現在錯誤後面。如何解決OAuth 2.0中的access_token

公告:未定義的屬性:stdClass的:: $中的access_token C:\ XAMPP \ htdocs中\谷歌\ OAuth的\ validate.php上線46

這是我的代碼:

<?php 
//setting parameters 
$authcode= $_GET["code"]; 
$clientid='my'; 
$clientsecret='my'; 
$redirecturi='https://localhost/google/oauth/validate.php'; 
$fields=array(
'code'=> urlencode($authcode), 
'client_id'=> urlencode($clientid), 
'client_secret'=> urlencode($clientsecret), 
'redirect_uri'=> urlencode($redirecturi), 
'grant_type'=> urlencode('authorization_code') 
); 
//url-ify the data for the POST 
$fields_string=''; 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
$fields_string=rtrim($fields_string,'&'); 
//open connection 
$ch = curl_init(); 
//set the url, number of POST vars, POST data 
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); 
curl_setopt($ch,CURLOPT_POST,5); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
// Set so curl_exec returns the result instead of outputting it. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
//to trust any ssl certificates 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
//execute post 
$result = curl_exec($ch); 
//close connection 
curl_close($ch); 
//extracting access_token from response string 
$response= json_decode($result); 
$accesstoken= $response->access_token; 
//passing accesstoken to obtain contact details 
$xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default /full?oauth_token='.$accesstoken); 
//reading xml using SimpleXML 
$xml= new SimpleXMLElement($xmlresponse); 
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); 
$result = $xml->xpath('//gd:email'); 
foreach ($result as $title) { 
echo $title->attributes()->address . "<br><br>"; 
} 
?> 
+0

[?你嘗試過什麼(http://mattgemmell.com/2008/12/08/what-have-you-tried/) – 2012-03-09 14:58:19

+0

我試圖讓用戶登錄谷歌聯繫人。 – SANSCLAW 2012-03-09 15:01:32

回答

相關問題