Sunday, October 21, 2018

Salesforce Integration with BOX

This Post will help you to integrate with BOX application and creating folders and storing files in BOX.
Note: We need to register an account in BOX.

Steps for integrate ssalesforce with BOX:
-> Here we are assuming salesforce as a service consumer and BOX as a service provider.

Step 1: we need to create an application(connected app) in Box.com, for this we need to go www.developer.box.com.

Always we have to create a connected app in service provider. So to create a connected app, click on create new app -> click on custom app -> click on next.
We are creating the user authentication, which requires username and password to log into the box for users. so choose standard OAuth 2.0 (user authentication) and click on next.

Give an Unique App name and click on create app.


The moment we get created we will get client ID, client Secret, and Redirect URI as shown below.

https://account.box.com/api/oauth2/authorize is the URL of the Box login page. To begin the process of authenticating and authorizing an application to work with the Box APIs, send an HTTP request like the following:

Step 2: Create a visualforce page to get the response of BOX.
So that we will give this page in redirect URI in BOX application as shown below.

Step 3: Store Client ID, Client Secret and Redirect URI in a custom object.

Step 4: Send a request to BOX.com to get a response like authorization code, for this we need to give parameters like

https://account.box.com/api/oauth2/authorize?response_type=code&client_id=<MY_CLIENT_ID>

&redirect_uri=<MY_REDIRECT_URL>&state=<MY_SECURITY_TOKEN>

i) EndPoint URI, ii) Client ID, iii) Response_Type, iv) State.


public class Rest_OAuth_Box { public string code {set; get;} public string accesstoken {set; get;} public Serverdata__c data {set; get;} public Rest_OAuth_Box(){ data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'BOX']; } /* Request the code from Box * Endpoint URL : https://account.box.com/api/oauth2/authorize * Method : GET * Parameters : response_Type = code * : client_ID * : redirect_url = callback url registered with box * : state = any random string */ public PageReference requestcode(){ string endpoint='https://account.box.com/api/oauth2/authorize?response_type=code&'; endpoint=endpoint+'client_id='+ data.Client_ID__c; endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c; endpoint=endpoint+'&state=nitajshaik'; PageReference p = new PageReference(endpoint); return p; } public void readcode(){ code=Apexpages.currentPage().getparameters().get('code'); }
}



Step 5: As we trying to make a request to third party system, we have to register the third party system url
in remote site settings.


Step 6: Create a visualforce page for getting the response of code into that page.

<apex:page controller="Rest_OAuth_Box"> <apex:form> <apex:commandButton value="code" action="{!requestcode}"/> </apex:form> </apex:page>
execute this we will get CODE as show below.

Step 7: Send a reuqest for access token from BOX, with the help of the CODE get from above.

public class Rest_OAuth_Box { public string code {set; get;} public string accesstoken {set; get;} public Serverdata__c data {set; get;} public Rest_OAuth_Box(){ data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'BOX']; } /* Request the code from Box * Endpoint URL : https://account.box.com/api/oauth2/authorize * Method : GET * Parameters : response_Type = code * : client_ID = connected app clientid * : redirect_url = callback url registered with box * : state = any random string */ public PageReference requestcode(){ string endpoint='https://account.box.com/api/oauth2/authorize?response_type=code&'; endpoint=endpoint+'client_id='+ data.Client_ID__c; endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c; endpoint=endpoint+'&state=nitajshaik'; PageReference p = new PageReference(endpoint); return p; } public void readcode(){ code=Apexpages.currentPage().getparameters().get('code'); } /* Request access token from BOX * End Point URL : https://api.box.com/oauth2/token * Method : POST * Parameters : grant_type = authorization_code * : code = fetched in the above step * : client_ID = connected app clientid * : client_secret = connected app clientsecret */ public void requestToken(){ string url = 'https://api.box.com/oauth2/token'; string body = 'grant_type=authorization_code&'; body = body+'code='+ code; body = body+'&client_id='+ data.Client_ID__c; body = body+'&client_secret='+ data.Client_Secret__c; Http p = new http(); HttpRequest request = new HttpRequest(); request.setEndpoint(url); request.setMethod('POST'); request.setBody(body); HttpResponse response = p.send(request); String result = response.getBody(); System.JSONParser JP = JSON.createParser(result); while(JP.nextToken()!=Null){ if(JP.getText()=='access_token'){ JP.nextToken(); accesstoken = JP.getText(); } } } }

Step 8: Create response visualforce page to get the access token on page.


<apex:page controller="Rest_OAuth_Box" action="{!readcode}"> {!code} <apex:form > <apex:commandButton value="gettoken" action="{!requestToken}"/> {!accessToken} </apex:form> </apex:page>

After executng we will get accesstoken as shown below:




Step 9: Creating a folder in BOX
public class Rest_OAuth_Box { public string code {set; get;} public string result {set; get;} public string accesstoken {set; get;} public Serverdata__c data {set; get;} public Rest_OAuth_Box(){ data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'BOX']; } /* Request the code from Box * Endpoint URL : https://account.box.com/api/oauth2/authorize * Method : GET * Parameters : response_Type = code * : client_ID = connected app clientid * : redirect_url = callback url registered with box * : state = any random string */ public PageReference requestcode(){ string endpoint='https://account.box.com/api/oauth2/authorize?response_type=code&'; endpoint=endpoint+'client_id='+ data.Client_ID__c; endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c; endpoint=endpoint+'&state=nitajshaik'; PageReference p = new PageReference(endpoint); return p; } public void readcode(){ code=Apexpages.currentPage().getparameters().get('code'); } /* Request access token from BOX * End Point URL : https://api.box.com/oauth2/token * Method : POST * Parameters : grant_type = authorization_code * : code = fetched in the above step * : client_ID = connected app clientid * : client_secret = connected app clientsecret */ public void requestToken(){ string url = 'https://api.box.com/oauth2/token'; string body = 'grant_type=authorization_code&'; body = body+'code='+ code; body = body+'&client_id='+ data.Client_ID__c; body = body+'&client_secret='+ data.Client_Secret__c; Http p = new http(); HttpRequest request = new HttpRequest(); request.setEndpoint(url); request.setMethod('POST'); request.setBody(body); HttpResponse response = p.send(request); String result = response.getBody(); System.JSONParser JP = JSON.createParser(result); while(JP.nextToken()!=Null){ if(JP.getText()=='access_token'){ JP.nextToken(); accesstoken = JP.getText(); } } create(); } /* Create a folder in box * endpoint : https://api.box.com/2.0/folders * Authorization : Bearer ACCESS_TOKEN" * Body : name, parent{"name":"New Folder", "parent": {"id": "0"}}' * Method : POST */ public void create(){ string endpoint = 'https://api.box.com/2.0/folders'; string body = '{"name":"Nitaj", "parent": {"id": "0"}}'; Http p = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(endpoint); request.setMethod('POST'); request.setHeader('Authorization','Bearer '+ accesstoken); request.setBody(body); HttpResponse response = p.send(request); result = response.getBody(); } }


<apex:page controller="Rest_OAuth_Box" action="{!readcode}">
    {!code}
    <apex:form >
        <apex:commandButton value="gettoken" action="{!requestToken}"/>
        {!result}
    </apex:form>
</apex:page>




Step 10: Deleting a folder in BOX

public void deletefolder(){ string endpoint = 'https://api.box.com/2.0/folders/56198983784'; Http p = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(endpoint); request.setMethod('DELETE'); HttpResponse response = p.send(request); result = response.getBody(); }





No comments:

Post a Comment