![](/img/trans.png)
[英]PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client'
[英]OAuth2 returns invalid_client error
美好的一天,
我在獲取訪問令牌方面遇到了麻煩。 我按照這里的指南: http : //developers.box.com/oauth/已經獲取我的client_id , client_secret ,並在應用程序設置( 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以獲取訪問令牌,但是我被困在這個部分,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。
任何解決方案,想法為什么會發生這種情況?
謝謝您的幫助。
問題出在你正在設置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;
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.