We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Vivek K.2
10 years agoNew member | Level 1
Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?
Hi All,
I tried to upload the file into Drop Box account From Salesforce Using /files_put Dropbox Api.But am always getting following Error : [Status=Bad Request, StatusCode=400]{"error": "Body may not be empty"}.
Hereby My Code as follows,
Please advise.
Thanks,
Vivek.K
5 Replies
Replies have been turned off for this discussion
- Greg-DB10 years ago
Dropbox Community Moderator
First, you're using basic auth with your app key and secret, but this endpoint requires access to a specific Dropbox account, which is accomplished using an account-specific access token. You can find more information on using OAuth with the Dropbox API here:
https://www.dropbox.com/developers/reference/oauthguide
If you need to implement the OAuth 2 flow manually, this blog post may be helpful:
https://www.dropbox.com/developers/blog/45/using-oauth-20-with-the-core-api
The actual error you're getting indicates that the server didn't receive any data in the request's body. It looks like you're trying to set it using request.setBodyAsBlob, but it appears that didn't work, since the server didn't receive anything. You'll need to debug your usage of HttpRequest to determine why nothing is getting sent up.
- Vivek K.210 years agoNew member | Level 1
Thanks for your suggestion Gregory.
If it's possible could you please share some /files_put api working code using apex.
- Vivek K.210 years agoNew member | Level 1
Hi Gregory,
I updated my code like below,even though am getting Error : [Status=Bad Request, StatusCode=400]{"error": "Body may not be empty"}.
public class DropboxController
{
public Blob FileBody{get;set;}
public DropboxController()
{
}
public PageReference DropAuth()
{
HttpRequest request = new HttpRequest();
request.setMethod('POST');
request.setTimeout(60000);
request.setEndpoint('https://api-content.dropbox.com/1/files_put/auto/Test.txt');
Blob val = csvFileBody;
Blob accesstoken = Blob.valueOf('<redacted>');
String AccToken = 'Bearer ' + EncodingUtil.base64Encode(accesstoken);
request.setHeader('Authorization', AccToken);
request.setBodyAsBlob(val);
System.debug(val);
Http hp = new Http();
HttpResponse response = hp.send(request);
System.debug(' RESP ::: ' +response +''+ response.getBody());
return null;
}
}Can i have your suggestion .
Thanks,
Vivek K
- Greg-DB10 years ago
Dropbox Community Moderator
[Cross-linking for reference: https://stackoverflow.com/questions/36997863/unable-to-upload-the-file-into-dropbox-from-salesforce-using-files-put-api ]
I don't have any sample code for Apex specifically unfortunately.
I see you've updated your code to set the OAuth 2 access token. It looks like you're encoding it though? It shouldn't be encoded, and should just be passed up to the API as the original string.
By the way, I redacted your access token, but you should revoke it to be safe since you posted it publicly.
Have you been able to troubleshoot the your code.HTTP client to check what data it is or isn't actually sending up?
- Vivek K.210 years agoNew member | Level 1
Thanks for your response @Gregory.
I got the solution.Hereby my working code as follows,
public class DropboxController
{
//Fetched from URL
Public String code ;
Public String accesstoken;
public DropboxController()
{
code = ApexPages.currentPage().getParameters().get('code') ;
//Get the access token once we have code
if(code != '' && code != null)
{
AccessToken() ;
}
}
public PageReference DropAuth()
{
system.debug('UPLOAD'+accessToken );
//Authenticating
// PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=XXXX&redirect_uri=https://c.cs20.visual.force.com/apex/DropboxPage&state=Mytesting') ;
return null;
}
public void AccessToken()
{
//Getting access token from dropbox
String tokenuri = 'https://api.dropbox.com/1/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.cs20.visual.force.com/apex/DropboxPage';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('XXXX' + ':' + 'YYYY');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
JSONParser parser = JSON.createParser(resp);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token') {
accesstoken = parser.getText();
}
}
}
system.debug('accessToken==>'+accessToken );
System.debug(' You can parse the response to get the access token ::: ' + resp);
string token = 'https://api-content.dropbox.com/1/files_put/auto/ProjectListNew.csv';
HttpRequest r = new HttpRequest();
r.setEndpoint(token);
r.setHeader('Authorization','Bearer ' +accesstoken);
r.setMethod('PUT');
r.setTimeout(60000);
List<Project2__c> projlist = [Select id,name,Project_Name__c,Project_Type__c,Project_Status__c from Project2__c LIMIT 10];
string header = 'Record Id, Project Key, Project Name, Project Type, Project Status \n';
string finalstr = header ;
for(Project2__c a: projlist)
{
string recordString = '"'+a.id+'","'+a.Name+'","'+a.Project_Name__c+'","'+a.Project_Type__c+'","'+a.Project_Status__c+'"\n';
finalstr = finalstr +recordString;
}
Blob csvBlob = Blob.valueOf(finalstr);
r.setBodyAsBlob(csvBlob);
Http h1 = new Http();
HttpResponse res1 = h1.send(r);
string resp1 = res1.getBody();
System.debug('Upload Success :: ' + resp1);
}
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!
{
public DropboxController()
{
}
public PageReference DropAuth()
{
string body= 'Test';
List<Account > acclist = [Select id,name , CreatedDate , lastModifiedDate from Account limit 10];
string header = 'Record Id, Name , Created Date, Modified Date \n';
string finalstr = header ;
for(Account a: acclist)
{
string recordString = '"'+a.id+'","'+a.Name+'","'+a.CreatedDate+'","'+a.LastModifiedDate +'"\n';
finalstr = finalstr +recordString;
}
blob csvBlob = Blob.valueOf(finalstr);
System.debug('CSV :' + finalstr);
HttpRequest request = new HttpRequest();
request.setMethod('POST');
request.setEndpoint('https://content.dropboxapi.com/1/files_put/auto/Test.csv');
Blob headerValue = Blob.valueOf('5na5z4tgwqgqee3' + ':' + '<redacted>');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
request.setBodyAsBlob(csvBlob);
System.debug(request);
Http hp = new Http();
HttpResponse response = hp.send(request);
System.debug(' RESP ::: ' + response.getBody());
return null;
}
}