Dropbox API V2上传文件错误PHP-Laravel

时间:2018-02-08 07:17:03

标签: php dropbox dropbox-api dropbox-php dropbox-sdk

这是PHP中的库代码,用于使用laravel框架在dropbox-sdk(file- Client.php)中上传文件。

我会在我的Dropbox控制器中调用以下函数。

function uploadFile($path, $writeMode, $inStream, $numBytes = null)
{
    Path::checkArgNonRoot("path", $path);
    WriteMode::checkArg("writeMode", $writeMode);
    Checker::argResource("inStream", $inStream);
    Checker::argNatOrNull("numBytes", $numBytes);

    // If we don't know how many bytes are coming, we have to use chunked upload.
    // If $numBytes is large, we elect to use chunked upload.
    // In all other cases, use regular upload.
    if ($numBytes === null || $numBytes > self::$AUTO_CHUNKED_UPLOAD_THRESHOLD) {
        $metadata = $this->_uploadFileChunked($path, $writeMode, $inStream, $numBytes,
                                              self::$DEFAULT_CHUNK_SIZE);
    } else {
        $metadata = $this->_uploadFile($path, $writeMode,
            function(Curl $curl) use ($inStream, $numBytes) {
                $curl->set(CURLOPT_POST, true);
                $curl->set(CURLOPT_INFILE, $inStream);
                $curl->set(CURLOPT_INFILESIZE, $numBytes);
            });
    }

    return $metadata;
}

反过来使用此功能。

 private function _uploadFile($path, $writeMode, $curlConfigClosure)
  {
    Path::checkArg("path", $path);
    WriteMode::checkArg("writeMode", $writeMode);
    Checker::argCallable("curlConfigClosure", $curlConfigClosure);

    $url = $this->buildUrlForGetOrPut(
        $this->contentHost,
        $this->appendFilePath("2/files/upload", $path),
        $writeMode->getExtraParams());

    $curl = $this->mkCurl($url);

    $curlConfigClosure($curl);

    $curl->set(CURLOPT_RETURNTRANSFER, true);
    $response = $curl->exec();

    if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response);

    return RequestUtil::parseResponseJson($response->body);
}

我最近将网址从V1升级到V2。我现在收到此错误,无法找到此错误的根本原因。

enter image description here

我甚至尝试对Dropbox网址进行硬编码并将其放入curl请求但没有用。任何建议,帮助甚至领导都是一种生命保护。

1 个答案:

答案 0 :(得分:0)

Dropbox API v2不是API v1的替代品,因此您无法交换新的v2 API端点网址。如果您尝试更新API v1库以用于API v2,则需要进行大量代码更改。

例如,在您共享的示例中,文件上载的所需路径直接在v1端点的URL路径上提供,但这对API v2不起作用,这就是您收到该错误的原因

在API v2中,路径将作为Dropbox-API-Arg请求标头中的JSON值提供。您可以在文档中找到有关如何格式化API v2请求的信息:

https://www.dropbox.com/developers/documentation/http/documentation#files-upload

使用已经为Dropbox API v2构建的新库可能更容易,而不是尝试更新旧库。没有正式的Dropbox API v2 PHP SDK,但是有一些社区库可以使用此处列出的PHP的API v2:

https://www.dropbox.com/developers/documentation/communitysdks