You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Accessing Dropbox Using REST API in Salesforce


By: Radha

Dropbox allows the user to store and access the files and share with anyone on the fly. Once you have saved files on Dropbox, you can access them from anywhere from your account with the help of Internet. We can also synchronize our account to devices by the app. It also allows us to access to the authorized customer’s files via our Mobile Apps / Web application through REST API.

Features: 

  1. We can store our files on our account once and can access it anywhere across all your devices.
  2. Devices are synchronized automatically once if the app is installed.
  3. We can share our files to anyone.
  4. We can save videos, photos, presentation, and many more to your Dropbox.
  5. Our files are safe and no need to maintain manual backups.
  6. We can also have business / enterprise account and share files across our teams efficiently.
  7. Dropbox provides free storage files for individuals and further can extend on pricing as their needs and requirements.

Now, it’s possible to integrate Dropbox with Salesforce that provides access to your customer’s files.

Prerequisites for Dropbox  

  • Create an account in dropbox and verify the account by using the registered email address.

Figure 1: Create an dropbox account 

  • Click Create app and follow the steps given below.
  1. Choose an API as Dropbox API

Figure 2: Choose Dropbox API 

  1. Choose the access type either as Full Dropbox or a particular folder that you are going to provide access for file storage from Salesforce.

Figure 3: Choose the access type 

  1. Provide the name for your app. (ex. Salesforce_Metadata).

Figure 4: Name the app 

  1. Note down the App key and App secret.
  1. Redirect is the URI where you are going to launch the Visualforce page URL (Say for ex. https://<your-domain>.visual.force.com/apex/<visualforcepage>)

Figure 5: Settings 

Steps to be completed in Salesforce

  1. Create a Visualforce Page (ex. Dropbox) and a CommandButton to redirect to Dropbox to provide Single Sign On

  1. Create an Apex Controller (e.x. DropboxController_AC)
  1. Define the required constants needed for the REST API to collaborate with the Dropbox

  1. Define a method to execute the web service to get the response for the request provided with endpoint, parameters, headers, and body of the request.

Collaborating with Dropbox – Endpoints explained

REFERENCE for Endpoints to build REST API

  • Authorization

The button on the Visualforce page redirects to Dropbox account. Once the user signs into their account, it redirects back to the Visualforce page along with the state we provided and code generated from Dropbox.

Endpoint: https://www.dropbox.com/1/oauth2/authorize

HTTP METHOD: GET

PARAMETERS:

response_type = code / token;

client_id = App key;

redirect_uri = Salesforce Visualforce page;

state = arbitrary data (max 500 bytes);

  • Token Generation

Once the authorization is done, it is redirected to our Visualforce page.

Endpoint: https://api.dropboxapi.com/1/oauth2/token

HTTP METHOD: POST

PARAMETERS:

code = code returned as parameter in query string;

grant_type  = authorization_code

client_id = App key;

client_secret = App Secret;

redirect_uri = Salesforce Visualforce page;

 

SAMPLE RESPONSE:

  • Accessing Dropbox

Once the access_token is returned, all requests should be made using the token in request header.

  1. Get Account information of Logged in user

Endpoint: https://api.dropboxapi.com/1/account/info

HTTP METHOD: GET

HEADERS:

Authorization = Bearer <your-access-token>

SAMPLE RESPONSE:

  1. Upload a file

Endpoint: https://content.dropboxapi.com/1/files_put/auto/<directory/filename.extesion>

HTTP METHOD: PUT

HEADERS:

Authorization = Bearer <your-access-token>

BODY:

file = binary data of your file uploaded

 

SAMPLE RESPONSE:

Conclusion 

Though Dropbox is a file storage app on cloud, there might be a controversy that why we don’t use the Salesforce itself for file saving options. Of course, the controversy exists but it’s just another favorable solution for the users to save their files directly in Dropbox.