在虚拟机里,oschina API V5接口 授权登录时获取access_token返回false

serenehaly 发布于 2017/10/12 17:31
阅读 261
收藏 0
  • 问题描述:oschina授权登录,获取access_token,http请求为302,得到的结果为false;若不在虚拟机环境下则能获取到access_token,这是为什么呢?
  • 生产环境:VMware下的win10
  • 实现步骤:

1.用户授权登录后,在回调页面通过$_SERVER['QUERY_STRING'],获取code的值;以下为授权页代码:

$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$redirect_uri = 'http://localhost/gitee/callback'; //回调url

$url = 'https://gitee.com/oauth/authorize?client_id='.$client_id.'&redirect_uri='.$redirect_uri.'&response_type=code';
echo '<a href="'.$url.'">开源中国登录</a>';

2.在回调页面通过curl调用获取access_token的url,但结果却返回false;以下为回调页面代码:

$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$redirect_uri = 'http://localhost/gitee/callback';

$code = substr($_SERVER['QUERY_STRING'], 5);

$next_url = 'https://gitee.com/oauth/token?grant_type=authorization_code&code='.$code.'&client_id='.$client_id.'&redirect_uri='.$redirect_uri.'&client_secret='.$client_secret;
$post_data = array ();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $next_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);

var_dump($output);
var_dump(json_decode($output, true)['access_token']);
  • 结果截图:

结果返回false

 

 

加载中
返回顶部
顶部