cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

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: 

Salesforce To dropbox file upload

Salesforce To dropbox file upload

Tejas_99
Explorer | Level 3

Hello All,

 

I am trying to upload a file from salesforce to Dropbox.

 

I have created a class in salesforce as per suggestions from the internet.

 

My code is:

 

public class dropBoxApi{
Public String code ;
Public String accesstoken;
public String fileBody{get;set;}


public dropBoxApi()
{
code = ApexPages.currentPage().getParameters().get('code') ;
System.debug('Code='+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={clientId}&redirect_uri=http...') ;

return null;
}

public void AccessToken()
{
//Getting access token from dropbox


String appKey='************';
String appSecreatKey='**********';
String tokenuri = 'https://api.dropbox.com/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri=https:...';
//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('{appKey}' + ':' + '{appSecreatKey}');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug('response='+resp);
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('accessToken:'+accessToken );
System.debug(' You can parse the response to get the access token ::: ' + resp);

}
}

 

 

can anybody tell me what is 'code' value at line 

code = ApexPages.currentPage().getParameters().get('code') ;

 

Help is highly appreciated.

3 Replies 3

Greg-DB
Dropbox Staff
It looks like the 'code' variable here would be the "authorization code" used during the OAuth app authorization "code" flow.

You can find more information on how this works in the OAuth guide:

https://www.dropbox.com/developers/reference/oauth-guide

and the OAuth documentation:

https://www.dropbox.com/developers/documentation/http/documentation#authorization

Tejas_99
Explorer | Level 3

Hi Greg,

Thanks for the reply.

 

Can you tell me where will I get authorization code?

 

As you can see in code I already have value for "code" variable before making any request to dropbox.

I already have app key and secret key and I can generate access key by clicking on generate access key button in app console but I don't know from where I will get authorization code.

Greg-DB
Dropbox Staff
The authorization code is given from Dropbox during the OAuth app authorization flow. It would either be returned on the redirect URI, if used, or directly to the user on the Dropbox web site, if not. Please refer to the links in my last post for more information on how this works.

The authorization code is a temporary code used to get an access token. Once you have an access token for the user, you no longer need an authorization code for the user.

Also, if you only need to connect to your own account, you can just use the access token retrieved via the "Generate" button on the App Console as you mentioned. That directly returns an access token for your app and account, without going through the OAuth app authorization flow and getting an authorization code.
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Tejas_99 Explorer | Level 3
What do Dropbox user levels mean?