2

Please help me regarding integration between dropbox and salesforce ,

This Below link very useful to me : http://forceguru.blogspot.in/2014/05/dropbox-authentication-in-salesforce.html

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":"krCeDZGxqoAAAAAAAAAg9ejz6i3bsgPfCmvpvZF1pR0cRH_u-NhQ_IjtVeR9Gwa", "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' + ':' + '9ek5zgp4hbxkjjq');
        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

  • You're missing a space after Bearer on this line: r.setHeader('Authorization','Bearer' +accesstoken); – Ray Dehler Jul 21 '15 at 16:53
3

I see two problems regarding authorization.

  1. For basic level authorization, the keyword is Basic not BASIC. Its case sensitive.

  2. For any type of authorization a space is required between keyword and param. So a space is required after Bearer.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.