cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more 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: 

Access Token

Access Token

lonehero
Explorer | Level 4

Is there Any way to generate Access Token without Authorization Code. Actually I was Implementing it in Salesforce without using Visual force Page.... And what is Expiration of the Access Token... Here is my  code

 

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=<REDACTED>&redirect_uri=https://c.ap1.visual.force.com/apex/DropboxPage&state=Mytesting');
return pg;
}

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

Blob headerValue = Blob.valueOf('<REDACTED>' + ':' + '<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 txtdata='';
try {
string data = 'https://content.dropboxapi.com/2/files/download';
HttpRequest re = new HttpRequest();
re.setEndpoint(data);
re.setHeader('Authorization' , 'Bearer ' +accesstoken);
re.setHeader('Dropbox-API-Arg', '{ "path":"/Test.txt" }');
re.setMethod('POST');
re.setTimeout(60000);
Http h2 = new Http();
HttpResponse res2 = h2.send(re);
system.debug('body----->'+res2.getBody());
txtdata = res2.getBody();
}
catch(exception e) {
system.debug('error-----'+e.getMessage()+'line number--------->'+e.getLineNumber());
}
return txtdata;
} 
1 Reply 1

Greg-DB
Dropbox Staff

The Dropbox API supports two different OAuth 2 authorization flows: "code" and "token". The code flow requires the use of an authorization code. The token flow does not. The token flow is generally meant for client-side applications though, and does require a redirect URI, where the access token is return on the URL fragment (and so not sent to the server). You can find more information on the two different flows in the documentation.

Dropbox API access tokens do not expire by default, but they can be revoked by the user or app at any time.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?