我能够获得授权代码,但是我无法让令牌交换工作。我得到的响应是NULL。
任何人都对此有任何见解,我已经搞乱了几个小时。
function tokenRequest($clientId, $clientSecret, $code){
$url = 'https://www.strava.com/oauth/token?';
$oauthFields = array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'code' => $code);
$parameters = http_build_query($oauthFields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$parameters);
curl_setopt($ch, CURLOPT_POST, count($oauthFields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$urlResponse = curl_exec($ch);
curl_close($ch);
return (json_decode($urlResponse));
}
if (isset($_GET['code'])){
$code=$_GET['code'];
echo "code is: " . $code . "</br>";
$token = tokenRequest($clientId, $clientSecret, $code);
var_dump($token);
}
else{echo "User denied access request";}
答案 0 :(得分:1)
因此,在查看错误消息(谢谢Kevin)之后,我将CURL_SSL_VERIFYPEER选项从TRUE更改为FALSE。这行代码现在看起来像这样:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
还应该注意我正在使用XAMPP,如果其他人遇到这个问题。