【问题标题】:OAuth2 returns invalid_client errorOAuth2 返回 invalid_client 错误
【发布时间】:2013-12-06 02:40:33
【问题描述】:

美好的一天,

我在获取访问令牌时遇到问题。我在这里遵循了指南:http://developers.box.com/oauth/ 并且已经获取了我的 client_idclient_secret,并在 App 设置(OAuth2 参数)部分设置了 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.) 输入登录详细信息并允许授予我的应用访问权限后。我现在被重定向到我在应用设置 (something.php) 中设置的 redirect_uri,在此文件中,它将执行 curl post 以获取访问权限令牌,但我卡在这部分,卷曲结果返回错误:

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

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

任何解决方案,想法为什么会发生这种情况?

感谢您的帮助。

【问题讨论】:

    标签: php curl oauth box-api


    【解决方案1】:

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

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

    【讨论】:

    • 你让我开心了!!
    【解决方案2】:

    这是我在 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;
    
        },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-13
      • 2017-04-24
      • 2014-07-09
      • 2020-05-15
      • 2016-01-04
      • 2019-08-31
      • 2022-06-25
      相关资源
      最近更新 更多