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
vivek krish 4vivek krish 4 

Genearte the CSV File & Upload into DropBox Account Via Apex ?

Hi All,                    
           When End User Click the Command button need to genearte the dynamic CSV File using apex and uploading into DropBox account.how can i acheive this.  

Please advice.
Thanks,
Vivek.K
Best Answer chosen by vivek krish 4
vivek krish 4vivek krish 4
Hi All.
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=5na5z4tgwqgqee3&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('5na5z4tgwqgqee3' + ':' + 'xnpy19zr575uw9l');
        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);
   }
}

All Answers

vivek krish 4vivek krish 4
Herebu 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/home');
        Blob headerValue = Blob.valueOf('5na5z4tgwqgqee3' + ':' + 'xnpy19zr575uw9l');
        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;
    }
    
   
}

Error : {"error": "Body may not be empty"}

Please advice..where am doing wrong ?

Thanks,
Vivek.K
vivek krish 4vivek krish 4
Hi All.
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=5na5z4tgwqgqee3&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('5na5z4tgwqgqee3' + ':' + 'xnpy19zr575uw9l');
        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);
   }
}
This was selected as the best answer
krishna Bidwai 54krishna Bidwai 54
what's the value of ' code ' fetched from the apexpages()..
Tejas BodheTejas Bodhe
What is the value of 'code' at this line?
ApexPages.currentPage().getParameters().get('code') ;

I have tried both secret key and app key which I got from Dropbox app console.
But I get "code doesn't exist or has expired" error.