Last Updated: July 17, 2023
·
2.202K
· Khor

How to Use OAuth to Get File List from Dropbox

Goal

We wanted to figure out the easiest way to use OAuth to get file list from Dropbox on behalf of a Dropbox user using Javascript. Here is how we did it with OAuth.io.

Code Snippet

Try this snippet. Once you log in with your Dropbox account it will get the list of files in your account. Feel free to tweak it to get file list from different folders, etc.

https://jsfiddle.net/p187m5ur/6/

Code Explanation

HTML

<a id="dropbox-button" class="btn btn-block btn-social btn-dropbox">
  <i class="fa"></i> Get file list
</a>

CSS

None

Javascript

var params = {
  "path": ""
};
$('#dropbox-button').on('click', function() {
  // Initialize with your OAuth.io app public key
  OAuth.initialize('OAUTH_IO_PUBLIC_KEY');
  // Use popup for oauth
  // Alternative is redirect
  OAuth.popup('dropbox').then(provider => {
    console.log('provider:', provider);
    alert("You can check Dropbox provider object in browser console.");

    provider.post({
      url: "/files/list_folder",
      data: JSON.stringify(params),
      headers: {
         "Content-Type": "application/json"
      }
    }).then(data =>             {
      console.log('Files:', data);
      alert("You can find file list in browser console");
    });
  });
})

External Requirements

This Javascript code has external requirements:

Step-by-step Guide

Here is a summary of the steps to create this social button to get file list from Dropbox on a user's behalf using OAuth2.

  1. Create a Dropbox account, if you do not have one
  2. Create an app on Dropbox using this simple step-by-step guide
  3. Copy your Dropbox app client id and client secret
  4. Signup for OAuth.io account
  5. Link your Dropbox app with OAuth.io by pasting your Dropbox app client id and client secret into OAuth.io
  6. Copy the OAuth javascript snippet (https://jsfiddle.net/p187m5ur/6/) on to your web page

References

Use OAuth to get file list from Dropbox: https://tome.oauth.io/providers/dropbox/get-file-list