美好的一天,
我在获取访问令牌方面遇到了麻烦.我按照这里的指南:http://developers.box.com/oauth/ 已经获取我的client_id,client_secret,并在应用程序设置(OAuth2参数)部分设置redirect_uri.
这是client.php文件的代码
这里是文件的代码something.php(这是redirect_uri将去的地方)
'authorization_code', "code" => $code, "client_id" => $client_id, "client_secret" => $client_secret ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $post_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_params); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Accept: application/json' )); $data = curl_exec($ch); curl_close($ch); $json = json_decode($data, true); var_dump($json); ?>
......现在发生的事情是,
1.)在第一步(client.php)上,有一个带有提交按钮的表单.
2.)点击提交按钮后,我会被重定向到Box的登录页面,并带有"授权"按钮.
3.)输入登录详细信息并允许授予我的应用访问权限.我现在被重定向到我在应用程序设置(something.php)上设置的redirect_uri,在这个文件中,它将执行curl post以获取访问令牌,但是我被困在这个部分,curl结果返回有错误:
array(2) { ["error"]=> string(14) "invalid_client" ["error_description"]=> string(34) "The client credentials are invalid" }
我确定我已经正确地输入了我的client_id和client_secret,这是我从应用程序设置中获得的.并且redirect_uri的url 也启用了SSL.
任何解决方案,想法为什么会发生这种情况?
谢谢您的帮助.
问题出在您设置的cURL标头中something.php
。卸下Content-Type
标题。实际上,您根本无法设置标题-cURL将发送正确编码的参数,而Box默认会返回JSON数据。
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json' ));