Showing posts with label SFDC & Box Integration. Show all posts
Showing posts with label SFDC & Box Integration. Show all posts

Monday, September 23, 2019

Box API : How to Regenerate Access Token from Refresh Token

Box and Salesforce can be integrated to store files on box and link them with salesforce records. You can also write apex script to extract files or attachments from Salesforce and upload it into box folder.

For basic understanding on how to generate access token from box, refer below URL:
Box and Salesforce Integration

Whenever we perform handshake with box using OAuth, box returns access token along with refresh token. Below is sample JSON response for access token:

{
"access_token": "h91xxxxxxxxxxxxxxxxxxxx8",
"expires_in": 4012,
"restricted_to": [],
"refresh_token": "bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxM",
"token_type": "bearer"
}

here expires_in specifies the duration for which this access token is valid. After this duration, user will recieve 401 error so we need to regenerate the access token using refresh token.

Below is apex code which can be used to regenerate new access token and refresh token. In order to regenerate you need clientid, client secret and refresh token.

Above mentioned code is self explainatory.


Hope this help!!!


Monday, June 18, 2018

Search Content on Box from Salesforce Using Content Search API

In this blog, I am going to share the code snippet which can be used to do callout from Apex in order to search content on Box. You need to have access token in order to perform this callout.

To learn how to get access token from box, please refer Box and Salesforce Integration

Box provide content search API through which you can search for files and folders present in Box.
I have created a wrapper class in apex which will be used to parse the JSON response from Box.

Below is code snippet:

You can call this static method and pass the parameters to get results.

Hope this will help!!

Monday, February 20, 2017

Integrating Box File Picker with Salesforce

Box File Picker allows to view content present in box folders and to integrate it with other application. Suppose we want to display the box folder contents for user from salesforce UI, then we can use javascript code snippet provided for box file picker.

Box File Picker allow your users to share their Box content with your application — without using OAuth!

Now we are going to use below code sample to implement this in Salesforce. In order to use Box File Picker, you need Client Id or API Key.
Navigate to your box application and click on Edit Application button.

Suppose your URL is -https://app.box.com/developers/services/edit/133000

Then Client Id is 133000




Below is VF Page Code


Explanation about different attributes in Box file picker:
  • linkType : The type of link. Can be direct or shared
    • shared : Allow you to select folder as well as file inside folder. Shared also allow you to specify the access type of file or folder like people with link, people in your company or people with access to folders
    • direct : Allow you to select only files not folder.
  • multiselect : true or false.
    • true: will get checkboxes for multiple file selection
    • false: will get radio button for single file selection

Callback & Responses from Box File Picker

Below javascript functions helps in integrating Box file picker with your application:

// Register a success callback handler
boxSelect.success(function(response) {
    console.log(response);
});
// Register a cancel callback handler
boxSelect.cancel(function() {
    console.log("The user clicked cancel or closed the popup");
});

Details about callback functions:

  • Success function : Whenever user select files or folders, response is passed as JSON in sucess callback handler.
  • Cancel function : This function will be called whenever user click cancel or close the box file picker
Below is sample JSON response which we will get in success callback function for shared link type:


[
    {
        "url" : "https://app.box.com/s/jxi6hcwcvvbbccjka6k8ewhi3",
        "name" : "Happy Time",
        "access" : "collaborators",
        "type" : "folder",
        "id" : 100344004694
    },
    {
        "url" : "https://app.box.com/s/jdfaddssdc234232df123aad",
        "name" : "The Sunil Kumar.docx",
        "access" : "collaborators",
        "type" : "file",
        "id" : 1000000344
    }
]


How to fetch files from Box and store it into salesforce

You can pass the JSON to controller method using javascript remoting and parse JSON to find out file and folder information. JSON response return the file URL which can be used to get file details by doing callout and storing it as attachment in salesforce.

Please refer Fetching File from External/Public URL for more details on this.



BOX AND SALESFORCE INTEGRATION
ACCESS TOKEN USING OAUTH 2.0 IN SALESFORCE
REST API TUTORIAL FOR SALESFORCE

Sunday, June 21, 2015

Box and Salesforce Integration

In this blog, I am going to explain how to integrate Box.com and Salesforce using Apex and VF in order to upload document to Box and to view files present in Box in VF page.

First of all, Create a Box Application


Create Box content application which will help us to upload files directly from Salesforce to Box.com.  After creating app, copy client_id and client_secret values and specify your VF page URL as redirect_uri in Oauth 2 parameters section.


Now create another application and select Box view application. This will help us to view files in VF page. View API allow to convert files in HTML format for easy display in web pages. Copy view API Key from General information section.


Now Create a custom setting in Salesforce to store client_id,client_secret, access token, refresh token and View API key.



Now Create Remote site setting in order to do call out to Box.com.


Now create a VF page "boxAuthPage" and apex class "boxAuthPageController". Code is provided in end of this blog. Please scroll down to copy code and paste in your VF and apex class.

Snapshot of VF page:

Store client_id, client_secret, view api key in custom setting record. While creating custom setting record, I have specified "Box_sunil02kumar' as name for custom setting. If you want to use other name, then replace "Box_sunil02kumar' in apex class with your custom setting name.

Open VF page and click on Authorize Box button, system will redirect you to login page where you need to enter you box login credential and then a click grant access to box button.

All folder present in box will be listed in Folders drop down. When you select any particular folder, all files present in that folder will appear in page block table. When you click on "View" link, file will open on right side of page.

If you have box account, then use below url to test the functionality. Create folders in your box account and upload pdf and doc files in it. If you upload files with already existing file name in Box folder, then you will get error.

https://skforce-developer-edition.ap1.force.com/skforce__boxauthpage

I have built this page for demo. So every time you refresh this page, you need to again authorize it by clicking on Authorize box button. Actually I am not storing access token or refresh token or any user credentials and also scope of these variables are same as that of VF page. Also client id, client secret and access tokens are not displayed in demo page but if you follow all steps mentioned above including custom setting and remote site setting, then you will get complete UI.

Important information which will help you to understand apex class:
  • Uploading data into box.com uses content type as multipart/form-data.
  • You can not send a file to server which is expecting a binary. So you need to encode the base64 in binary supported format.
  • In order to view document, first you need to send request to get temporary download URL by sending file id to Box.com. Temporary download URL is returned in response Location header. This URL can be used to download file.
  • Once you get temporary download URL, sent request to BOX.com to get temporary document id. In HTTPRequest, you need to pass temporary download URL in body.
  • Now this document id which you get in response can be used to generate session id for file which will be used to view file in iframe in VF page. When you do call out to get session id, pass temporary document id in request body.
  • If file is processed by Box.com and is ready to view, then in response you will get session id. If your request is accepted but file is not yet converted to HTML format then in response header, you will time duration after which file will be ready. This information is present in 'retry' header in response.
  • Once you get file session id from Box.com, then you use it view file in iframe in your VF page by using  URL: https://view-api.box.com/1/sessions/'+fileSessionId+'/view


Notes:
  • You can upload any type of file to box folder.
  • Before uploading any file, please select folder in which you want to upload file.
  • As per my knowledge, View API doesn't support jpg and png images. 
  • You can easily view any pdf or word file from Box in SFDC using view api.
  • You can also specify download URL. Please refer Box content view API for this.
  • In apex, code is specified to get file download URL in FindFileDownloadUrl method in apex class.
This blog will help you to basic understanding of how to achieve integration between Box and SFDC by using apex code. After going through this code, you can easily modify the code as per your requirements.

Code for VF and apex class: