无法使用PHP API上传文件到Dropbox?

4

我正在尝试使用Dropbox API上传文件,但以下代码显示了一些错误:

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

代码作者: BenTheDesigner-Dropbox

错误: 致命错误: Uncaught exception 'Dropbox\Exception',错误信息为:尝试访问应用程序文件夹(sandbox)失败,因为此应用程序未配置为拥有应用程序文件夹。您的访问类型应该是“dropbox”吗? (状态码:403),文件位置:C:\wamp\www\BenTheDesigner-Dropbox-b49576c\Dropbox\OAuth\Consumer\Curl.php,行号:103

( ! ) Dropbox\Exception: 尝试访问应用程序文件夹(sandbox)失败,因为此应用程序未配置为拥有应用程序文件夹。您的访问类型应该是“dropbox”吗? (状态码:403),文件位置:C:\wamp\www\BenTheDesigner-Dropbox-b49576c\Dropbox\OAuth\Consumer\Curl.php,行号:103

我不知道该怎么办?


我已经正确输入了我的两个密钥。 - Frank
2个回答

7
目前在Dropbox API中,您可以为应用程序注册两种“访问类型”,即“应用文件夹”(又名沙盒)和“完整的Dropbox”(又名dropbox)。您在注册应用程序时做出此选择。之后,您可以在应用程序的选项页面上查看其类型。您可以在此处找到到应用程序选项页面的链接: https://www.dropbox.com/developers/apps 在您的代码中,您需要设置您的“根目录”(通常与您的密钥设置在同一位置)以反映正确的访问类型。
这个错误意味着您的应用程序已经注册了完整的Dropbox访问权限,但是您在代码中设置了应用文件夹。
要解决此问题,请找到设置根目录的位置并将其切换为“dropbox”。 (或者,如果确实要使用应用文件夹访问权限,请使用应用程序文件夹访问权限注册新应用程序并使用那些密钥。)
看起来你正在使用的库在API类中有一个“setRoot”函数,你应该使用它: https://github.com/BenTheDesigner/Dropbox/blob/master/Dropbox/API.php

我对你的回答非常满意。我的应用程序类型是“完整的Dropbox”。请告诉我在哪里可以找到我的应用程序根目录路径? - Frank
我已经完成了,在API.php的第45行设置了路径。public function __construct(OAuth\Consumer\ConsumerAbstract $OAuth, $root = 'dropbox') - Frank

3
在API.php文件中,需要对这一行进行更改。
public function __construct(OAuth\Consumer\ConsumerAbstract $OAuth, $root = 'dropbox')
    {
        $this->OAuth = $OAuth;
        $this->setRoot($root);
    }

$root='dropbox'中,您的代码中有sandbox .. 所以请删除sandbox并将其替换为dropbox。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接