We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

sarfaraz_k's avatar
sarfaraz_k
New member | Level 2
2 years ago

Salesforce - Dropbox API Connection

Please help me regarding integration between dropbox and salesforce ,

 

I want to get user details from dropbox and display it in salesforce.

Another requirement is, upload files from salesforce and upload it in dropbox . and get the files or images from dropbox and display it in salesforce....

I have access token ,api key, api secret all things , but I don't know how pass request and how to get response as user details.

In below Code ... in system debug I m getting

{ access_token":"REDACTED", "token_type": "bearer", "uid": "328256643"}

But in system.debug of Account Information, I m getting following error- Account Information ::

{"error": "OAuth 2 \"Authorization\" header is not well-formed."}

Below is Apex Class:

public class DropboxController
{

    //Fetched from URL
    String code ;
    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()
    {
        //Authenticating
        PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=nw7mfi19tfaxak8&redirect_uri=https://c.ap2.visual.force.com/apex/Dropbox&state=Mytesting') ;
        return pg ;
    }

    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.ap2.visual.force.com/apex/Dropbox'; 
        HttpRequest req = new HttpRequest();
        req.setEndpoint(tokenuri);
        req.setMethod('POST');
        req.setTimeout(60*1000);

        Blob headerValue = Blob.valueOf('nw7mfi19tfaxak8' + ':' + 'REDACTED');
        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.dropbox.com/1/account/info';
        HttpRequest r = new HttpRequest();
        r.setEndpoint(token);
        r.setHeader('Authorization','Bearer' +accesstoken);
        r.setMethod('GET');
        r.setTimeout(60000);
        Http h1 = new Http();
        HttpResponse res1 = h1.send(r);
        string resp1 = res1.getBody();

        System.debug(' Account Information :: ' + resp1);
   }
}

As I am new to integration any help will be grateful...

Thanks


  • sarfaraz_k wrote:

    ...

    But in system.debug of Account Information, I m getting following error- Account Information ::

    {"error": "OAuth 2 \"Authorization\" header is not well-formed."}

    ...

    ...
            string token = 'https://api.dropbox.com/1/account/info';
            HttpRequest r = new HttpRequest();
            r.setEndpoint(token);
            r.setHeader('Authorization','Bearer' +accesstoken);
            r.setMethod('GET');
    
    ...

    ...


    Hi sarfaraz_k,

    Take in mind that 'Authorization' header' value should contain its type and the value. These are 2 different things that have to stay different (i.e. with separator between). As far as can be seen you have stick them together. That's why the format doesn't go well. 😉 To avoid such mistakes use some of the official SDKs or prototype your calls using API explorer.

    By the way you're using unsupported call; better use 2/users/get_current_account, for instance.

    Good luck.


  • sarfaraz_k wrote:

    ...

    But in system.debug of Account Information, I m getting following error- Account Information ::

    {"error": "OAuth 2 \"Authorization\" header is not well-formed."}

    ...

    ...
            string token = 'https://api.dropbox.com/1/account/info';
            HttpRequest r = new HttpRequest();
            r.setEndpoint(token);
            r.setHeader('Authorization','Bearer' +accesstoken);
            r.setMethod('GET');
    
    ...

    ...


    Hi sarfaraz_k,

    Take in mind that 'Authorization' header' value should contain its type and the value. These are 2 different things that have to stay different (i.e. with separator between). As far as can be seen you have stick them together. That's why the format doesn't go well. 😉 To avoid such mistakes use some of the official SDKs or prototype your calls using API explorer.

    By the way you're using unsupported call; better use 2/users/get_current_account, for instance.

    Good luck.

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Engineer rankDropbox Engineer

    Здравко is correct. I would suggest following his guidance.

     

    By the way, you shared your access token, I went ahead and redacted that information.