You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

lonehero's avatar
lonehero
Explorer | Level 4
6 years ago

Access Token

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;
} 
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,917 PostsLatest Activity: 11 days ago
334 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!