function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumant Kuchipu 1Sumant Kuchipu 1 

Salesforce-Box using BOX SDK API issue

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.

 
Dilip_VDilip_V
Sumanth,

It seems you havn't created remote site settings for box.

Go to
Setup->Security->Remote site settings

Add this url as end point URL
https://api.box.com/oauth2/token

for more info
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_remotesitesetting.htm

Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.
Sumant Kuchipu 1Sumant Kuchipu 1
Thanks Thermos, it helped me but I'm getting another error
18:25:18:261 USER_DEBUG [129]|DEBUG|response body: {"error":"invalid_grant","error_description":"Refresh token has expired"}

Still not understanding whats wrong here and don't know how to refresh token..
public static void updateBoxCustomSetting(BoxApiConnection api){
        try{
            List<Cust_Sett__c> bx=[SELECT id,Access_Token__c, Refresh_Token__c FROM Cust_Sett__c 
                                                    where name='MyCustSett' limit 1] ;
            //api.refreshToken();
            bx[0].Access_Token__c=api.getAccessToken();
            bx[0].Refresh_Token__c=api.getRefreshToken();
            update bx[0];
            
        }catch(exception e){
            system.debug('*********Exception while updating custom setting:'+e.getmessage());
        }
    }



 
Sumant Kuchipu 1Sumant Kuchipu 1
I got solution on this issue and that solution helped me lot that I don't even maintain access tokens and refresh tokens..
i.e. Named Credentials in Salesforce, this is really great feature.

Thanks..
Thankachan ThomasThankachan Thomas
Hi Sumant,

I am using BoxApiConnection API, and noticed that every time when I initialize as api = new BoxApiConnection(clientId, clientSecret, accessToken, refreshToken); it is refreshing the accessToken and refreshToken values. My understanding is accessToken is valid for an 1 hr, and refreshToken is for 60 days according to Box documentation. Have you come across this situation. You mentioned that you have used Named Credentials rather than custom settings to store tokens. Have you Named Credentials with BoxApiConnection (box-salesforce-sdk) or with BOX REST API? Could you please share the detailed solution with bit of sample code to see how you achevied this?

Thanks
Tom
Suman KuchSuman Kuch
Yes, Im using Box Rest API not using SDK, i think this is perfect rather managing tokens.
Thankachan ThomasThankachan Thomas
Thanks Suman.
Thankachan ThomasThankachan Thomas
Hi Suman,

I need bit of your help. I am trying to use Named Credentials for Box integration and kind of stuck. My reqirement is to move file from one folder to another folder.

In the Named Credentials I have specified Url as https://api.box.com/2.0/

Now I need to help with the apex code, could you please share bit of code which you actally did either it is folder creation or anything. I just tried simple as follows to get a folder info and it is not working.
 
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('callout:MyBoxForSalesforce/folders/' + folderId);
            
HttpResponse response = http.send(req);
            
system.debug('HttpResponse=>' + response);

Also I have created Auth Provider in Salesforce with the Box client_id and client_secret, and selected Open ID Connect as Provider Type, followed https://help.salesforce.com/articleView?id=admin_files_connect_box_auth.htm&language=en_US&type=0.

Could you please share anything else I need to setup based on your expereice.

Really appreciate your help on this.

Thanks
Tom




 
Thankachan ThomasThankachan Thomas
Hi Suman,

I got it working.. thanks anyway.

Cheers,
Tom
Namita ChoudharyNamita Choudhary
Hi Thankachan ,

Can you please explain that what did you do to got it working fine ?

Thanks !
 
Sowmya MadaSowmya Mada
Hey Sumant,

I am tryiing to integrate sallesforcw eith box and want to upload a file to box using box sdk.
Can you explain how did you get the fileID which you ahve passed in  "BoxFile file = new BoxFile(api, '034192123421');"?

Thanks for he help in advance 1.

Regards,
Sowmya
rkris320rkris320
Folks here mention that they got a solution, but never bothered to post here the solution steps.  Even a short write-up could help lot of people's times. Sumanth, Tom: Can you post what that actually helped you?  I am trying to connect to Box to get a file downloaded to Salesforce. Again, I want to do like a one-time setup of client id/secret and the token to connect to Box.  Appreciate your help, if some one could share a short write-up based on their experience.  If I solve it, I will sure do the write-up here.  Thanks 
KiranKumar Punuru 39KiranKumar Punuru 39
@Thankachan Thomas,@Sumant Kuchipu 1 - Iam facing issue in named credentials upon clicking save its not doing anything just blank screen is displaying could you please adivise?