使用 PHP 的 Dropbox SDK

Dropbox SDK using PHP

我正在尝试使用 Dropbox PHP 设置基本身份验证示例,但我失败了。当我启动 index.php 时,我被重定向到保管箱登录。然后,一旦我登录,我将被重定向回 dropbox_finish.php 并且我有 statecode 例如:

?state=bKwCAATDb7A5JLHmCj8wgA%3D%3D&code=YQb8c261HlAAAAAAAAAAXAK1OOk6DqK-RtqnJ_SG0F0

然后我得到这个错误:

[Mon May 25 00:27:39.074399 2015] [:error] [pid 5459] [client 127.0.0.1:44489] PHP Fatal error: Uncaught exception 'Dropbox\Exception_BadRequest' with message 'HTTP status 400\n{"error_description": "Invalid client_id or client_secret", "error": "invalid_client"}' in /var/www/php/oauth/vendor/dropbox/dropbox-sdk/lib/Dropbox/RequestUtil.php:250\nStack trace:\n#0 /var/www/php/oauth/vendor/dropbox/dropbox-sdk/lib/Dropbox/WebAuthBase.php(39): Dropbox\RequestUtil::unexpectedStatus(Object(Dropbox\HttpResponse))\n#1 /var/www/php/oauth/vendor/dropbox/dropbox-sdk/lib/Dropbox/WebAuth.php(270): Dropbox\WebAuthBase->_finish('YQb8c261HlAAAAA...', 'https://oauth.d...')\n#2 /var/www/php/oauth/web/dropbox_finish.php(4): Dropbox\WebAuth->finish(Array)\n#3 {main}\n thrown in /var/www/php/oauth/vendor/dropbox/dropbox-sdk/lib/Dropbox/RequestUtil.php on line 250

一段时间以来,我一直在尝试设置一些尽可能基本的东西,但没有结果。任何帮助将不胜感激。

这是我的代码index.php:

include_once __DIR__.'/../app/start.php';
include_once __DIR__.'/../app/dropbox_auth.php';

然后start.php:

session_start();
$_SESSION['user_id'] = 1;
require_once __DIR__.'/../vendor/autoload.php';
$key = "fttwagu78r37ped";
$secret = "fttwagu78r37ped";
$GLOBALS['app_name'] = "oauth-php/1.0";
$GLOBALS['redirectURI'] = "https://oauth.dev/dropbox_finish.php";
$GLOBALS['HomeURI'] = "https://oauth.dev";

$appInfo = new Dropbox\AppInfo($key, $secret);

//CSRF Cross Site Request Forgery protection.
$csrfTokenStore = new Dropbox\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');

$webAuth = new Dropbox\WebAuth($appInfo, $GLOBALS['app_name'], $GLOBALS['redirectURI'], $csrfTokenStore);

$db = new PDO('mysql:host=localhost;dbname=oauth','root','root');

$user = $db->prepare("SELECT * FROM user WHERE id = :user_id");
$user->execute(['user_id' => $_SESSION['user_id']]);
$user = $user->fetchObject();

文件dropbox_auth.php:

if ($user->token){
    $client = new Dropbox\Client($user->token, $GLOBALS['app_name'], 'UTF-8');
    $client->getAccountInfo();    
} else {
    $authURL = $webAuth->start();
    header("Location: $authURL");
    exit();
}

Dropbox 完成文件 dropbox_finish.php:

require_once "../app/start.php";
list($accessToken) = $webAuth->finish($_GET);
$store = $db->prepare("UPDATE user SET token = :dropbox_token"
        . "WHERE id = :user_id");
$store->execute(['dropbox_token' => $accessToken,
        'user_id' => $_SESSION['user_id']]);
header("Location: ".$GLOBALS['HomeURI']);

作为额外的细节,只是说我在 MySQL:

中有这个
+--------------------+
| Database           |
+--------------------+
| oauth              |

+-----------------+
| Tables_in_oauth |
+-----------------+
| user            |
+-----------------+

+----+--------------------+-------+
| id | username           | token |
+----+--------------------+-------+
|  1 | xxxx@yyyyyyyyy.com |       |
+----+--------------------+-------+

我做错了什么? 谢谢

您确定您的客户端 ID 或客户端密码正确吗?该错误清楚地表明其中一个是错误的。