phpdropboxdropbox-apidropbox-php

Dropbox SDK using PHP


I am trying to setup a basic authentication example using Dropbox PHP and I am failing. When I launch my index.php I am redirected to dropbox login. Then once I login, I am redirected back to dropbox_finish.php and I have state and code e.g.:

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

Than I get this error in:

[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

I have been trying to set something as basic as possible to start with for a while now with no results. Any help will be appreciated.

This is my code index.php:

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

Then 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();

The file 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 finish file 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']);

As additional detail, just to say that I have this in MySQL:

+--------------------+
| Database           |
+--------------------+
| oauth              |

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

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

What am I doing wrong? thx.


Solution

  • Are you sure your client id or client secret is correct? The error clearly says one of them is wrong.