简体   繁体   English

OAuth2返回invalid_client错误

[英]OAuth2 returns invalid_client error

good day, 美好的一天,

I'm having trouble with getting the access token. 我在获取访问令牌方面遇到了麻烦。 I've followed the guide here: http://developers.box.com/oauth/ and already get my client_id , client_secret , and set the redirect_uri on the App settings ( OAuth2 parameters ) section. 我按照这里的指南: http//developers.box.com/oauth/已经获取我的client_idclient_secret ,并在应用程序设置( OAuth2参数 )部分设置redirect_uri

Here is the code for the file client.php 这是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';
?>

and here's code for the file something.php (this is where the redirect_uri will go) 这里是文件的代码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';
?>

...now what happens is, ......现在发生的事情是,

1.) on the first step ( client.php ), there is a form there with submit button. 1.)在第一步( client.php )上,有一个带有提交按钮的表单。

2.) After i clicked on the submit button, i get redirected to the Box' login page with the button "Authorize". 2.)点击提交按钮后,我会被重定向到Box的登录页面,并带有“授权”按钮。

3.) after entering login details and allow granting access for my app. 3.)输入登录详细信息并允许授予我的应用访问权限。 I now gets redirected to the redirect_uri that i've set on the App settings ( something.php ), where in this file, it will execute curl post to get an access token, but i get stuck at this part, the curl result returns with the error: 我现在被重定向到我在应用程序设置( something.php )上设置的redirect_uri ,在这个文件中,它将执行curl post以获取访问令牌,但是我被困在这个部分,curl结果返回有错误:

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

I'm sure that I have entered my client_id and client_secret correctly which I've obtained from the App settings. 我确定我已经正确地输入了我的client_idclient_secret ,这是我从应用程序设置中获得的。 And the url for the redirect_uri , is also SSL enabled. 并且redirect_uri的url也启用了SSL。

Any solutions, ideas why this is happening? 任何解决方案,想法为什么会发生这种情况?

Thank you for your help. 谢谢您的帮助。

The problem is in the cURL headers you're setting something.php . 问题出在你正在设置something.php的cURL标题中。 Remove the Content-Type header. 删除Content-Type标头。 In fact, you can not set the headers at all - cURL will send the correctly encoded parameters and Box will return JSON data by default. 实际上,您根本无法设置标头 - cURL将发送正确编码的参数,Box默认会返回JSON数据。

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

Here is how I received the token in JS 这是我在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;

    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:Google plus Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:&#39;invalid_client&#39; - PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client' 在PHP中卷曲以获取access_token OAuth2返回错误:“ invalid_client” - curl in PHP to get access_token OAuth2 return error: “invalid_client” 未捕获的 Google_Auth_Exception:获取 OAuth2 访问令牌时出错,消息:&#39;invalid_client: Unauthorized&#39; - Uncaught Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_client: Unauthorized' fecing&#39;Google_Auth_Exception&#39;wror获取OAuth2访问令牌,消息:&#39;invalid_client&#39;&#39;错误 - fecing 'Google_Auth_Exception' wror fetching OAuth2 access token, message: 'invalid_client'' error invalid_client 向 Discord API 发送 OAuth2 请求时 - invalid_client When Sending OAuth2 Request to Discord API 致命错误:未捕获的异常“ Google_Auth_Exception”,消息为“获取OAuth2访问令牌时出错,消息:&#39;invalid_client&#39; - Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' 401. 这是一个错误。 错误:invalid_client 未找到 OAuth 客户端 - 401. That’s an error. Error: invalid_client The OAuth client was not found Spotify请求令牌返回invalid_client - Spotify request token returns invalid_client 使用带有 laravel/passport 的 PKCE 总是返回错误:“invalid_client”消息:“客户端身份验证失败” - Using PKCE with laravel/passport always returns error:“invalid_client” message:“Client authentication failed” 验证错误{“ErrorCode”:“invalid_client”,“错误”:“需要客户端标识符”} - Auth Error {“ErrorCode”:“invalid_client”,“Error”:“Client identifier is required”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM