• Sumant Kuchipu 1
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 4
    Replies
Hi, 
We have custom attachment which attach the files to BOX (using REST API) and rendering attachment from there when the user clicks on "View" link. But the following functionolity works on PDF and image files, they are directly opening on next tab/window but when we click on Docs or CSV, the file is downloading direclty rather opening  on new page. Please someone give me clues on this? see the following code snippet.
Apex Code: 
public String fileOpen(){
     HttpRequest req = new HttpRequest();
     req.setEndpoint(fileDownloadURL);
     req.setMethod('GET');
     Http binding = new Http();
     HttpResponse res = binding.send(req);
     Blob boxFile = res.getBodyAsBlob();
     String boxResponse = 'data:'+res.getHeader('Content-Type')+';base64,'+EncodingUtil.base64Encode(boxFile );
     return boxResponse ;
}
VisualForce Page:
<apex:page controller="BoxController" action="{!fileOpen}">
    <apex:form >
        <apex:outputText escape="false" value="{!boxResponse}"/> 
    </apex:form>
</apex:page>

Please help me on rendering doc/csv files on page rather downloading.. 

Appreciate your help..
Sumant K
 
Hi,

I'm trying to open/download a file returned from box.

when we click on a "Cleck Me" link, it calls the VF page and calls the Apex .. but I'm not sure how to open/download page from apex
Image and code are below.

User-added image

VF page:
<apex:page controller="BoxController" action="{!fileOpen}">

</apex:page>

Apex code:
 
try{
            String bFileId=String.valueof(ApexPages.CurrentPage().getparameters().get('bFileID'));

            String aToken = api.getAccessToken();
            if(bFileId!=null && bFileId!=''){
                String endPointURLToViewFile='https://api.box.com/2.0/files/'+bFileId+'/content';
                HttpRequest req = new HttpRequest();
                req.setEndpoint(endPointURLToViewFile);
                req.setMethod('GET');
                req.setHeader('Authorization', 'Bearer ' + aToken);
                req.setHeader('Content-Type','application/json');
                req.setHeader('Accept','application/json');
                req.setTimeout(120000);
                Http http = new Http();
                HTTPResponse res = http.send(req);
                
                String downloadURL=res.getHeader('Location');

            }
        }catch (Exception e){
            System.debug(' **** File Download Exceptions '+e);
        }
    }

Here I can get "downloadURL" but I don't know how to call this URL to opan/download with that URL.
Could someone please help me on this? 

Thanks,
Sumant K
 
Hi,

I have trying to do box integration with all approaches but nothinkg solved. The following approach seems to be easy but still not able to make call to box..
Basically we are trying this integration with Salesforce Box SDK Api (directly installed in salesforce platform) and trying to connect to box and get file Info (but actually we need upload and download, this SDK api has those methods to do that). Please find any clue on that issue and let me know. please look at this steps I followed

Step1: created box app for client id and client secret
2: got Access Token and Refresh Token using postman service
3. used these in following Apex code.
4. when I tried to Attach a file I get the following error.
 
Error:
19:36:28:488 USER_DEBUG [22]|DEBUG| Exception $$$$$$ An unexpected error occurred when trying to make a callout to the Box API. Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://api.box.com/oauth2/token

And the Apex code is...
 
public class BoxIntegrationClass{
    private static String CLIENT_ID='xxxxxx';
    private static String CLIENT_SECRET='yyyyy';
    private static String ACCESS_TOKEN='yyyyy';
    private static String REFRESH_TOKEN='yyyyy';
    @future (callout=true)
    public static void createAttachment(id attachmentid){
        System.debug('In Integration class  ');        
        BoxApiConnection api = new BoxApiConnection(CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN,REFRESH_TOKEN); //This is successfully executed
        System.debug('api ---- '+api); 
        Attachment myAttachment=[select name,body,parentid from attachment where id=:attachmentid];
        BoxFile file = new BoxFile(api, '034192123421'); //This is failing, the error is above 
        try{
        	String previewLink = file.getPreviewLink();
        	System.debug('file link '+previewLink); 
        }catch(Exception e){
            System.debug(' Exception $$$$$$ '+e.getMessage());
        }
    }
}
I don't understand why its failing... please help on this.