无法使用php api将文件上传到Dropbox?

问题描述 投票:4回答:2

我正在尝试使用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

错误:致命错误:未捕获异常'Dropbox \\ Exception',消息'App folder(沙盒)访问尝试失败,因为此应用程序未配置为具有应用程序文件夹。 您的访问类型应该是“dropbox”吗? (状态代码:403)'在第103行的C:\\ wamp \\ www \\ BenTheDesigner-Dropbox-b49576c \\ Dropbox \\ OAuth \\ Consumer \\ Curl.php

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

我不知道该怎么做?

dropbox dropbox-api dropbox-php
2个回答
7
投票

目前有两种“访问类型”可以注册Dropbox API应用程序,“app folder”(aka sandbox)和“full Dropbox”(又名dropbox)。 注册应用程序时,您可以选择此选项。 之后,您可以在应用的选项页面上查看您的类型。 您可以在此处找到指向应用选项页面的链接: https//www.dropbox.com/developers/apps

在您的代码中,您需要设置“root”(通常设置在与键相同的位置)以反映正确的访问类型。

此错误表示您的应用已注册完整的Dropbox访问权限,但您已在代码中设置了应用文件夹。

要解决此问题,请找到root设置的位置并将其切换为“dropbox”。 (或者,如果您确实想要使用应用程序文件夹访问权限,请注册具有应用程序文件夹访问权限的新应用程序并使用这些密钥

看起来你正在使用的库在你应该使用的API类中有一个函数“setRoot”: https//github.com/BenTheDesigner/Dropbox/blob/master/Dropbox/API.php


3
投票

在此行中更改API.php文件

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

$root='dropbox'和你的代码中有沙盒..所以删除沙箱和写Dropbox

© www.soinside.com 2019 - 2024. All rights reserved.