Forum Discussion

ckienquoc's avatar
ckienquoc
Explorer | Level 4
4 years ago
Solved

Cannot Download File by using API

I've tried to download a file using Dropbox API & PHP with the following code: 

 

 

 

                $out_fp = fopen($local_tmp_file_path, 'w+');
		
		$headers = array(	'Authorization: Bearer '. $DROPBOX_TOKEN,
					'Content-Type:',
					'Dropbox-API-Arg: '. json_encode(array(
					"path"=> '/file_data/tmp/2021_07/191.png'
				))
		);
		
		$ch = curl_init('https://content.dropboxapi.com/2/files/download');
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_FILE, $out_fp);
		
		$response = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		
		fclose($out_fp);

 

 

 

 

The code run, and I always received a file the content like "Error Something went wrong. Don't worry, your files are still safe and the Dropboxers have been notified. Check out our Help Center and forums for help, or head back to home" !?!?!.

 

I've tried to download other files, but the problem still same. 

 

I don't have trouble with upload file by the way. 

 

 

 

 

 

  • Greg-DB's avatar
    Greg-DB
    4 years ago

    Thanks, that's helpful. I see your client is passing up 'Content-Length: -1', which is incorrect. Mine is not doing that, so it may depend on what version of curl/PHP you're using.

     

    Try explicitly specifying a header of: 'Content-Length: 0'.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Based on the error message, it looks like the HTTP request may have been malformed, causing the call to fail.

     

    I see you're supplying an empty 'Content-Type:', but that may not be the cause. I just tried this code myself, plugging in my own test  access token and path, and it worked for me.

     

    Can you check that your $DROPBOX_TOKEN value is valid and doesn't contain any stray characters, such as whitespace, etc.?

     

    Otherwise, you can set the following to enable extra output for debugging:

     

    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

     

     

    If you share the output, be sure to redact your access token.

    • ckienquoc's avatar
      ckienquoc
      Explorer | Level 4

      This is the debug result. I've got error 400 [Bad request].  

      I've tried to run the same source on localhost environment and it works fine. 

      This code only have problem with download (Upload is OK) when I run it on Development server. 

       

      I also added the OAuth 2 Redirect Uris for both environments. One for localhost and one for https://my_website.com

       

      Please help! Thank You 

       

       

      * About to connect() to content.dropboxapi.com port 443 (#2)
      *   Trying 162.125.82.14...
      * Connected to content.dropboxapi.com (162.125.82.14) port 443 (#2)
      *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
        CApath: none
      * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
      * Server certificate:
      * 	subject: CN=content.dropboxapi.com,O="Dropbox, Inc",L=San Francisco,ST=California,C=US,serialNumber=4348296,incorporationState=Delaware,incorporationCountry=US,businessCategory=Private Organization
      * 	start date: Jan 28 00:00:00 2021 GMT
      * 	expire date: Feb 01 23:59:59 2022 GMT
      * 	common name: content.dropboxapi.com
      * 	issuer: CN=DigiCert SHA2 Extended Validation Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
      > POST /2/files/download HTTP/1.1
      Host: content.dropboxapi.com
      Accept: */*
      Authorization: Bearer MY_TOKENXXXXXXXXXXXXXXXXXXXX
      Content-Type: application/octet-stream
      Dropbox-API-Arg: {"path":"\/file_data\/shared\/IMPORT_DEPARTMENT_TMP_vn.xlsx"}
      Content-Length: -1
      Expect: 100-continue
      
      < HTTP/1.1 400 Bad Request
      < Content-Length: 14468
      < Content-Type: text/html
      < Vary: Accept-Encoding
      < X-Dropbox-Response-Origin: local
      < Date: Fri, 09 Jul 2021 02:44:18 GMT
      < Server: envoy
      < Connection: close
      < 
      * Closing connection 2

       

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        Thanks, that's helpful. I see your client is passing up 'Content-Length: -1', which is incorrect. Mine is not doing that, so it may depend on what version of curl/PHP you're using.

         

        Try explicitly specifying a header of: 'Content-Length: 0'.