OAuth2返回invalid_client错误


OAuth2 returns invalid_client error

你好,

我在获取访问令牌时遇到问题。我遵循了这里的指南:http://developers.box.com/oauth/并且已经获取了我的client_idclient_secret,并在App settings(OAuth2 parameters)部分设置了redirect_uri

以下是client.php文件的代码

<?php
    $client_id = 'my_client_id_here'; //removed
    $post_url = 'https://www.box.com/api/oauth2/authorize';
    include 'includes/header.php';
?>
    <div id="content">
        <form action="<?php echo $post_url; ?>" type="POST" enctype="application/x-www-form-urlencoded">
            <input type="text" name="response_type" value="code">
            <input type="text" name="client_id" value="<?php echo $client_id; ?>">
            <input type="text" name="state" value="vexhax97td8xf_SomeTemporaryValueForTesting">
            <input type="submit">
        </form>
        <div id="response"></div>
    </div>
<?php
    include 'includes/footer.php';
?>

下面是文件something.php的代码(这是redirect_uri将要去的地方)

<?php
$client_id =  'my_client_id_here'; //removed
$client_secret =  'my_client_secrect_here'; //removed
$post_url = 'https://www.box.com/api/oauth2/token';
$code = $_GET['code'];
include 'includes/header.php';
$fields_params = array(
        "grant_type" => '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);
?>
    <div id="content">
        <?php 
            //Nothing fancy, just for displaying passed values
            if (isset($_GET))
                var_dump($_GET); 
            if (isset($_POST))
                var_dump($_POST); 
        ?>
    </div>
<?php
    include 'includes/footer.php';
?>

现在发生的是

1.)在第一步(client.php)上,有一个带有提交按钮的表单。

2.)在我点击提交按钮后,我被重定向到Box的登录页面,并点击"授权"按钮。

3.)输入登录详细信息后,允许授予我的应用程序访问权限。我现在被重定向到我在应用程序设置上设置的redirect_urisomething.php),在这个文件中,它将执行curl post以获得访问令牌,但我在这一部分遇到了困难,curl结果返回错误:

array(2) { ["error"]=> string(14) "invalid_client" ["error_description"]=> string(34) "The client credentials are invalid" }

我确信我已经正确输入了从应用程序设置中获得的client_idclient_secretredirect_uri的url也启用了SSL。

为什么会发生这种情况,有什么解决方案和想法吗?

谢谢你的帮助。

问题出在您设置something.php的cURL标头中。卸下Content-Type标头。事实上,您根本无法设置头-cURL将发送正确编码的参数,Box将默认返回JSON数据。

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json'
));

以下是我在JS 中接收令牌的方式

authorizeUser = function(){    
        var results = $.ajax({
            // The URL to process the request
            url : 'https://www.box.com/api/oauth2/token',
            type : 'POST',
            data : {
                grant_type : 'authorization_code',
                code : data.boxAuthorizationCode,
                client_id : data.clientId,
                client_secret : data.clientSecret
            },
            beforeSend: function (xhr) {
  xhr.setRequestHeader("Authorization", "Bearer $token")
},
            dataType: "json",
            success: function(response) {
               //console.log(response);
               console.log(response.access_token);
               data.access_token = response.access_token;
               tokenGranted();
            }
        });
        return results.responseText;
    },