cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted

Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

New member | Level 1
New member | Level 1

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,

public class DropboxController
{
   
    
    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;
    }
    
   
}

Please advise.

Thanks,

Vivek.K

5 Replies 5
Highlighted

Re: Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

Dropboxer
Dropboxer

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.

Highlighted

Re: Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

New member | Level 1
New member | Level 1

Thanks for your suggestion Gregory.

 If it's possible could you please share some /files_put api working code using apex.

 

 

Highlighted

Re: Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

New member | Level 1
New 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

Highlighted

Re: Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

Dropboxer
Dropboxer

[Cross-linking for reference: https://stackoverflow.com/questions/36997863/unable-to-upload-the-file-into-dropbox-from-salesforce-... ]

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?

Highlighted

Re: Unable to Upload the File into DropBox From Salesforce Using /Files_put API ?

New member | Level 1
New 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_idXXX&redirect_uri=https://c.cs...;
        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=http...
        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);
   }
}

 

Polls
Do you know how to organize your files and folders?
We have created a guide on folder best practice, so you can get organized now check it out here.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?