Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to upload files to dropbox using php api?

I am trying to upload file using dropbox api but following Code shows some errors as:

bootstrap.php

// Register a simple autoload function
spl_autoload_register(function($class){
    $class = str_replace('\\', '/', $class);
    require_once('../' . $class . '.php');
});

// Set your consumer key, secret and callback URL
$key      = 'XXXXXXXXXXXXX';
$secret   = 'XXXXXXXXXXXXX';

// Check whether to use HTTPS and set the callback URL
$protocol = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
$callback = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// Instantiate the required Dropbox objects
$encrypter = new \Dropbox\OAuth\Storage\Encrypter('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$storage = new \Dropbox\OAuth\Storage\Session($encrypter);
$OAuth = new \Dropbox\OAuth\Consumer\Curl($key, $secret, $storage, $callback);
$dropbox = new \Dropbox\API($OAuth);

putFile.php

// Require the bootstrap
require_once('bootstrap.php');

// Create a temporary file and write some data to it
$tmp = tempnam('/tmp', 'dropbox');
$data = 'This file was uploaded using the Dropbox API!';
file_put_contents($tmp, $data);

// Upload the file with an alternative filename
$put = $dropbox->putFile($tmp, 'api_upload_test.txt');

// Unlink the temporary file
unlink($tmp);

// Dump the output
var_dump($put);

Code By: BenTheDesigner-Dropbox

Error: Fatal error: Uncaught exception 'Dropbox\Exception' with message 'App folder (sandbox) access attempt failed because this app is not configured to have an app folder. Should your access type be 'dropbox' instead? (Status Code: 403)' in C:\wamp\www\BenTheDesigner-Dropbox-b49576c\Dropbox\OAuth\Consumer\Curl.php on line 103

( ! ) Dropbox\Exception: App folder (sandbox) access attempt failed because this app is not configured to have an app folder. Should your access type be 'dropbox' instead? (Status Code: 403) in C:\wamp\www\BenTheDesigner-Dropbox-b49576c\Dropbox\OAuth\Consumer\Curl.php on line 103

i have no idea what to do?

like image 434
Frank Avatar asked Dec 28 '22 02:12

Frank


2 Answers

There are two kinds of "access type" you can currently register a Dropbox API app for, "app folder" (a.k.a. sandbox) and "full Dropbox" (a.k.a. dropbox). You make this choice when you register the app. Afterwards, you can see which type you have on your app's options page. You can find a link to your app's options page here: https://www.dropbox.com/developers/apps

In your code, you need to set your "root" (usually set in the same place as your keys) to reflect the correct access type.

This error means that your app is registered for full Dropbox access, but you've set app folder in your code instead.

To fix this, find where your root is set and switch it to 'dropbox'. (Alternatively, if you do want to use app folder access, register a new app with app folder access and use those keys.)

It looks like the library you're using has a function "setRoot" in the API class you should use: https://github.com/BenTheDesigner/Dropbox/blob/master/Dropbox/API.php

like image 126
Greg Avatar answered Jan 05 '23 12:01

Greg


change in API.php file in this line

public function __construct(OAuth\Consumer\ConsumerAbstract $OAuth, $root = 'dropbox')
    {
        $this->OAuth = $OAuth;
        $this->setRoot($root);
    }

in $root='dropbox' and in ur code there is sandbox .. so remove sandbox and write dropbox

like image 20
Cake PHP Avatar answered Jan 05 '23 12:01

Cake PHP