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

来源:互联网 发布:淘宝新店访客多少正常 编辑:程序博客网 时间:2024/05/18 06:23
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) :

view plainprint?
  1. public class DropboxController  
  2. {  
  3.     //Fetched from URL  
  4.     String code ;  
  5.       
  6.     public DropboxController()  
  7.     {  
  8.         code = ApexPages.currentPage().getParameters().get('code') ;  
  9.         //Get the access token once we have code  
  10.         if(code != '' && code != null)  
  11.         {  
  12.             AccessToken() ;  
  13.         }  
  14.     }  
  15.       
  16.     public PageReference DropAuth()  
  17.     {  
  18.         //Authenticating  
  19.         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') ;  
  20.         return pg ;  
  21.     }  
  22.       
  23.     public void AccessToken()  
  24.     {  
  25.         //Getting access token from dropbox  
  26.         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';   
  27.         HttpRequest req = new HttpRequest();  
  28.         req.setEndpoint(tokenuri);  
  29.         req.setMethod('POST');  
  30.         req.setTimeout(60*1000);  
  31.             
  32.         Blob headerValue = Blob.valueOf('vaabb5qz4jv28t5' + ':' + 'dpmmll522bep6pt');  
  33.         String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);  
  34.         req.setHeader('Authorization', authorizationHeader);  
  35.         Http h = new Http();  
  36.         String resp;  
  37.         HttpResponse res = h.send(req);  
  38.         resp = res.getBody();  
  39.           
  40.         System.debug(' You can parse the response to get the access token ::: ' + resp);  
  41.    }  
  42. }  


Step 3 : Now create visualforce page (DropboxPage) :

view plainprint?
  1. <apex:page controller="DropboxController">  
  2. <apex:form>  
  3.     <apex:pageblock>  
  4.         <apex:commandbutton action="{!DropAuth}" value="Dropbox Authentication">  
  5.     </apex:commandbutton></apex:pageblock>  
  6. </apex:form>  
  7. </apex:page>  


Step 4 : Replace the code with your values

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

view plainprint?
  1. 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 
view plainprint?
  1. 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.
0 0