【发布时间】:2013-12-06 02:40:33
【问题描述】:
美好的一天,
我在获取访问令牌时遇到问题。我在这里遵循了指南:http://developers.box.com/oauth/ 并且已经获取了我的 client_id、client_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_id 和 client_secret。 redirect_uri 的 url 也启用了 SSL。
任何解决方案,想法为什么会发生这种情况?
感谢您的帮助。
【问题讨论】: