0

This is library code in PHP for uploading a file in dropbox-sdk(file- Client.php), using laravel framework.

I would call below function in my dropbox controller.

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

which in turn uses this function.

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

I have recently upgraded urls to V2 from V1. I'm getting this error now an couldn't find root cause for this error.

enter image description here

I even tried to hardcode the dropbox url and put it in curl request but no use. Any suggestions, help or even lead is a life saver.

Pavan Tolety
  • 163
  • 2
  • 16

1 Answers1

0

Dropbox API v2 is not a drop-in replacement for API v1, so you can't just swap in the new v2 API endpoint URLs. If you are attempting to update an API v1 library for use with API v2, it will require significant code changes.

For instance, in the sample you shared, the desired path for the file upload was supplied directly on the URL path for the v1 endpoint, but that doesn't work for API v2, which is why you get that error.

In API v2, the path would be supplied as a value in JSON in the Dropbox-API-Arg request header. You can find information on how API v2 requests should be formatted in the documentation:

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

It may easier to use a newer library already built for Dropbox API v2 instead of trying to update an older one. There isn't an official Dropbox API v2 PHP SDK, but there are some community libraries for using API v2 from PHP listed here:

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

Greg
  • 16,359
  • 2
  • 34
  • 44