Hi All, In my previous post Connect Salesforce with Box.com (The Integration series) I have shared how can you create a Box.com app and authorize the user from Salesforce.
In this post I am sharing two utility method to create a folder on box.com and upload a file on box.com. There are various other methods are available but for starting I have found these two most useful.
So first create a folder in box.com platform you can use this method.
[code language="java"] private void createfolder(String fileName, string parentid) { String url = 'https://api.box.com/2.0/folders'; string authorizationHeader = 'Bearer '+Accesstoken ; HttpRequest req = new HttpRequest(); req.setheader('Authorization',authorizationHeader); req.setBody('{"name":"'+fileName+'", "parent":{"id":"0"}}'); req.setMethod('POST'); req.setEndpoint(url); Http h = new Http(); Httpresponse resp = h.send(req); System.debug(resp.getbody()+'-----------------------'+resp); } [code]
To upload a file in Box.com platform use this utility method.
[code language="java"] private void uploadFileCode(String folderId,Attachment file,String token){ String boundary = '----------------------------356sdf986eff'; String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+fileName+'";\nContent-Type: application/octet-stream'; String footer = '--'+boundary+'--'; String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n')); while(headerEncoded.endsWith('=')){ header+=' '; headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n')); } String bodyEncoded = EncodingUtil.base64Encode(file.body); Blob bodyBlob = null; String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length()); if(last4Bytes.endsWith('==')) { last4Bytes = last4Bytes.substring(0,2) + '0K'; bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes; String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded); } else if(last4Bytes.endsWith('=')) { last4Bytes = last4Bytes.substring(0,3) + 'N'; bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes; footer = '\n' + footer; String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded); } else { // Prepend the CR LF to the footer footer = '\r\n' + footer; String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded); } String sUrl = 'https://upload.box.com/api/2.0/files/content?parent_id='+folderId; HttpRequest req = new HttpRequest(); req.setHeader('Content-Type','multipart/form-data; boundary='+boundary); req.setMethod('POST'); req.setEndpoint(sUrl); req.setBodyAsBlob(bodyBlob); req.setTimeout(60000); req.setHeader('Authorization', 'Bearer '+token); req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size())); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getbody()+'-----------------------'+res); } [code]
These are the two methods which you can use to create a folder and upload file.
Do you have anything to add or like this post or any other thing to share please share them in comments.
Many Thanks. I knew only User/pwd Oauth2 using apex. It was great to learn this User Agent Oauth2 flow using apex.