我尝试使用OAuth2对LinkedIn API进行身份验证。代码:
if ((isset($_GET["code"])) AND (isset($_GET["state"]))) {
$code = $_GET["code"];
$state = $_GET["state"];
$curl_request = curl_init();
curl_setopt_array($curl_request, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://www.linkedin.com/uas/oauth2/accessToken",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
grant_type => "authorization_code",
code => "$code",
redirect_uri => "SECRET",
client_id => "SECRET",
client_secret => "SECRET"
)
));
$curl_result = curl_exec($curl_request);
var_dump($curl_result);
}
我收到了这条消息:
你可以帮忙吗?string(153)" {" error_description":"缺少必需参数, 包含无效参数值,参数不止一次。 : CLIENT_ID""错误":" INVALID_REQUEST"}"
答案 0 :(得分:4)
您需要在cURL POST调用中添加以下标头,以确保正确解释您在POST正文中传递的值:
内容类型:application / x-www-form-urlencoded
有关其他信息,请参阅http://tools.ietf.org/html/rfc6749#section-4.1.3了解实际的OAuth 2.0 RFC规范。
答案 1 :(得分:3)
使用: http_build_query
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.linkedin.com/uas/oauth2/accessToken',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query (array(
client_id => "secret",
client_secret => "secret",
grant_type => "authorization_code",
redirect_uri => "url",
code => $code
)),
));
答案 2 :(得分:0)
我对此感到疑惑,但我遇到了同样的问题,当我刚刚将客户端ID和秘密移动到POST数组的开头时,一切正常。我甚至没有。