Hi All, We all know that’s how costly it is to store data in Salesforce.com. So we always want to store data in third party so that we can reduce the cost. Thats why today I am sharing the authorization process between Salesforce and Box.com in my Integration series.
For this first you need to go to Box.com for a Developer account which you can create here.
After successful account creation you need to create an App to get the key and token. You can create an app here.
After creation your app will look like this.
Give your Visualforce page url as redirect URL. Now use the below code to perform the authorization process.
Controller:
[code language="java"] public with sharing class BoxComIntegration { //Fetched from URL private String code ; private string key = 'llpm**************2ks8'; private string secret = 'LMh**************jCmK2fDHK' ; private string redirect_uri = 'https://c.ap1.visual.force.com/apex/BoxComUpload'; public BoxComIntegration() { code = ApexPages.currentPage().getParameters().get('code') ; //Get the access token once we have code if(code != '' && code != null) { AccessToken() ; } } public PageReference BoxAuth() { //Authenticating PageReference pg = new PageReference(BoxAuthUri (key , redirect_uri)) ; return pg ; } public String BoxAuthUri(String Clientkey,String redirect_uri) { String key = EncodingUtil.urlEncode(Clientkey,'UTF-8'); String uri = EncodingUtil.urlEncode(redirect_uri,'UTF-8'); String authuri = ''; authuri = 'https://account.box.com/api/oauth2/authorize?'+ 'client_id='+key+ '&response_type=code'+ '&redirect_uri='+uri+ '&state=security_token%3D138r5719ru3e1%26urldfrgdgvrev'; return authuri; } public void AccessToken() { //Getting access token from google HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setEndpoint('https://api.box.com/oauth2/token'); String messageBody = 'code='+code+'&client_id='+key+'&client_secret='+secret+'&redirect_uri='+redirect_uri+'&grant_type=authorization_code'; req.setHeader('Content-length', String.valueOf(messageBody.length())); req.setBody(messageBody); req.setTimeout(60*1000); Http h = new Http(); String resp; HttpResponse res = h.send(req); resp = res.getBody(); Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res.getBody()) ; String token = String.valueOf(responseMap.get('access_token')); system.debug('***************'+token); } } [code]
VF Page:
[code language=”html”]
<apex:page controller="BoxComIntegration"> <apex:form > <apex:pageblock > <apex:commandbutton action="{!BoxAuth}" value="Authentication"> </apex:commandbutton></apex:pageblock> </apex:form> </apex:page>
[code]
Also add two Base URLs in remote site setting. And now you are all set when you run this code you will get access token and using that token you can easily upload, download the files.
Let me know if you like this post. Do you have anything to add in the post. let me know in comments section.
Also check second post of Integration series connect salesforce with box com part 2
Awsme and very fruitful post….
Hi, where do i find the ‘code’ value? what does it stands for..?
When we make a request to box.com server User first need to allow our app to access his data. When user allow our app then we get a long string of code. Thats code value we are using here.