May 9, 2014

Dropbox Authentication In Salesforce - Easy Setup (Authentication Part 1)

From log time managing data is a big deal, and a lot are concerned with it. Multiple options are available to handle this, out of them Dropbox, Box.com, Amazon are the main ones.

In this blog Authentication with Dropbox is explained so developers can get started.

Step 1 : Create an account on Dropbox, then go to this link and create an app. It will create "App Key" and "App Secret" which we will be using in Salesforce. Leave the OAuth2 section blank for now.


Step 2 : Go to salesforce and create this apex class (DropboxController) :

public class DropboxController
{
    //Fetched from URL
    String code ;
    
    public DropboxController()
    {
        code = ApexPages.currentPage().getParameters().get('code') ;
        //Get the access token once we have code
        if(code != '' && code != null)
        {
            AccessToken() ;
        }
    }
    
    public PageReference DropAuth()
    {
        //Authenticating
        PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=vaabb5qz4jv28t5&redirect_uri=https://c.ap1.visual.force.com/apex/DropboxPage&state=Mytesting') ;
        return pg ;
    }
    
    public void AccessToken()
    {
        //Getting access token from dropbox
        String tokenuri = 'https://api.dropbox.com/1/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.ap1.visual.force.com/apex/DropboxPage'; 
        HttpRequest req = new HttpRequest();
        req.setEndpoint(tokenuri);
        req.setMethod('POST');
        req.setTimeout(60*1000);
          
        Blob headerValue = Blob.valueOf('vaabb5qz4jv28t5' + ':' + 'dpmmll522bep6pt');
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        Http h = new Http();
        String resp;
        HttpResponse res = h.send(req);
        resp = res.getBody();
        
        System.debug(' You can parse the response to get the access token ::: ' + resp);
   }
}


Step 3 : Now create visualforce page (DropboxPage) :



    
        
    




Step 4 : Replace the code with your values

1 . As you can see we have this in code :

PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=vaabb5qz4jv28t5&redirect_uri=https://c.ap1.visual.force.com/apex/DropboxPage&state=Mytesting') ;


You have to replace "client_id" with your "App Key" (redirect_uri is the complete page URL which we've just created). Now as visualforce page is created you can fill the OAuth2 section as shown in the screenshot above in dropbox.

2. Replace "dpmmll522bep6pt" in this line
Blob headerValue = Blob.valueOf('vaabb5qz4jv28t5' + ':' + 'dpmmll522bep6pt');
with you "App Secret"

Step 5 : Don't forget to set the remote site settings for dropbox



Now all set to go. Open the page "https:// ..... /apex/DropboxPage" and hit "Dropbox Authentication". In the debug you will get the access token which you can further use to hit Dropbox APIs.

Please note, code is not beautified as this is just to explain how you can authenticate dropbox with salesforce. A lot more creativity can be applied here.

From here everything is set and you are ready to hit the dropbox API and fetch the data or store the data. Complete documentation is here.

Happy Coding!!

11 comments:

  1. Awsome...always be the fan of your work..:)

    ReplyDelete
  2. I keep getting an error 400
    Invalid redirect_uri: u'https://c.eu2.visual.force.com/apex/mytest'. It must exactly match one of the redirect URIs you've pre-configured for your app (including the path)

    ReplyDelete
    Replies
    1. In your DropBox, go to your app page and find the redirect uri and past this url : "https://c.ap1.visual.force.com/apex/DropboxPage" and Add this.

      Delete
  3. Superb... Its Awesome... :)

    Thank You... :)

    ReplyDelete
  4. I am getting an error when i hit on DropBox Authentication button
    error is -->Invalid redirect_uri: "https://c.ap1.visual.force.com/apex/lightingprefix__DropboxPage": It must exactly match one of the redirect URIs you've pre-configured for your app (including the path).

    ReplyDelete
    Replies
    1. In your DropBox, go to your app page and find the redirect uri and past this url : "https://c.ap1.visual.force.com/apex/DropboxPage" and Add this.

      Delete
  5. It's really amazing. you have provided us a valuable information. economical IT solutions

    ReplyDelete

  6. You're missing a space after Bearer on this line: r.setHeader('Authorization','Bearer' +accesstoken);

    ReplyDelete
  7. Very informative blog and very useful article thank you for sharing with us , keep posting learn more about salesforce training,salesforce online training

    ReplyDelete
  8. I have two questions:
    1. What is the code referring to? in line
    ApexPages.currentPage().getParameters().get('code') ;
    2. Can you share code of vf page

    ReplyDelete