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.


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()
        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?'+
        return authuri;
    public void AccessToken()
        //Getting access token from google
        HttpRequest req = new HttpRequest();
        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()));

        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'));

VF Page:

<apex:page controller="BoxComIntegration">
<apex:form >
    <apex:pageblock > 
        <apex:commandbutton action="{!BoxAuth}" value="Authentication">


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



4 thoughts on “Connect Salesforce with Box.com

    1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s