Connecting to DropBox API in WordPress (and Debugging)

I’ll keep this simple.

I am using the DropBox API to connect an application which uploads videos to Dropbox, concats them together and then uploads the finalized video back to the same folder.

No DropBox SDK is being used here. It is a simple connection with Drobox using PHP and HTTP calls.

Debugging (My Way)

Debug vars in your functions.php file

There doesn’t seem to be an easy way to output any errors to the browser console when coding a function in PHP within WordPress. I stumbled upon a useful function though, error_log. To utilize error_log, you must enable (set to “true”) WP_DEBUG_LOG in your wp-config.php file. At the end of the day, this works a lot better as I needed to see and check long arrays of data which wouldn’t be too pleasant to do in a browser console.

With that set, you can throw in any variables, any string, anything you need inside of that sucker and voila, you’ll see what you need in the debug.log file. By the way, you’ll find that in your wp-content folder.

I used error_log(“whatever here”); to view any errors that may occur in the upload or data snabbing process.

I mostly looked at the WP debug log to see what the response array was for my POST. And with that I could properly debug the code. 

Remember! Use print_r($your-array->data1->data2, true); to display your response array in a readable format within the debug log.

Making the POST

Set up your API

Set your api key as a variable so you can quickly and easily use it where needed and change it all at once if necessary.

$db_api = 'api-key';

Set your headers

$db_headers = array(
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . $db_api
);

Dropbox error: “invalid access token”

If you are getting this error from Dropbox, it could be that you have an expired token or you are missing the “Bearer ” text as seen above from your call.

Create your post

I’m using get_temporary_link as an example for a request. We don’t need to worry about Grizzly or anything like that since WordPress has a built in method for performing an HTTP request, wp_remote_post().

$video1 = wp_remote_post( 'https://api.dropboxapi.com/2/files/get_temporary_link', array(
    'headers' => $dp_headers,
    'body' => json_encode(array(
    'path' => "dropbox path"
)),
    'data_format' => 'body'
));

If you are getting an error (like Bad JSON) or find that the data isn’t being sent in the proper format with WordPress. The “data_format” parameter will fix this for you.

Remember! Be sure to format your PHP array correctly for converting to JSON. I had no interest in doing this myself for certain parts, so I used an online JSON to PHP array converter. I had to fix it up a little bit but still saved myself the headache of doing all the nesting manually. Aye! It works! 

Key Dropbox Endpoints

  • save_url – Upload files directly to DropBox using a url 
  • get_temporary_link – Get a temporary link that allows you to download a file for a set amount of time with the special link provided
  • get_metadata – I used this to get the length of the videos. Super useful!

 

That’s about it. Hope this helps. If you have any questions on anything, make a comment below. I’ll actually probably respond.

Thanks!

Share