繁体   English   中英

如何解决OAuth 2.0中的access_token

[英]how to solve access_token in OAuth 2.0

我正在努力获取登录的用户详细信息。 它出现在错误之后。

注意:第46行的C:\\ xampp \\ htdocs \\ google \\ oauth \\ validate.php中未定义的属性:stdClass :: $ access_token

这是我的代码:

<?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>";
}
?>

Google APIs PHP Client的示例应用程序演示了如何为经过身份验证的用户获取联系人列表。

该示例应用程序还演示了如何遍历OAuth 2.0流程并获得访问令牌。

范例: http : //code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM