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: 

Using previously acquired OAuth2 token with the Java SDK

Using previously acquired OAuth2 token with the Java SDK

Cory A.
New member | Level 1

I'm trying to get a list of folders in a users account to present as a list of choices to upload a photo to.  I already have a manually built OAuth2 flow working to allow a user to link their Dropbox account and I store the access token gained from that process.

I can use that token to successfully make calls to the REST API, and my code does not use the disable_access_token call, but when I try to use that access token with the Java SDK, I get an exception stating "The given OAuth 2 access token doesn't exist or has expired."

DbxRequestConfig config = new DbxRequestConfig("Piquero/0.1", null);  
DbxClient dropbox = new DbxClient(config, dla.getOAuth2AccessToken().toString());  
String folder = "/";  
listing = dropbox.getMetadataWithChildren(folder);


That last line there results in the exception, and it doesn't seem to matter what folder actually is. (My app requests access to all folders, but for only images & video files.)

Why is this happening?

3 Replies 3

Greg-DB
Dropbox Staff

Dropbox API access tokens don't automatically expire, but there are a few ways they can be revoked:

 

If none of these apply to your scenario, it's possible the access token string is getting altered somehow on your side. Have you tried logging out the access token both where it does work (i.e., when you first get it and use it with the HTTP API) and later when it doesn't work (i.e., when you try to use it with the Java SDK)?

Cory A.
New member | Level 1

Like I said, I can successfully make calls through HTTP calls to the REST API, so that rules out the token being altered. I have not used the disable_access_token endpoint. The Dropbox account I am testing with is my own, and I have not revoked access.

As I mentioned, I registered my app to be able to access any folder, not just the self-named one under /Apps/Piquero. However, just in case that was the issue, I tried both / and /AppsPiquero as arguments to DbxClient.getMetaWithChildren(). However the exception with the message mentioned above happens in both cases.

In the browser I click on a URL that has been mapped to a servlet in a Java webapp. That servlet does a bunch of data gathering and then uses HttpRequest.getRequestDispatcher() to forward the request onto a JSP. It uses the data to spit out the HTML for the browser in the usual MVC fashion.

The Dropbox Java SDK call which produces the exception happens in the servlet. Milliseconds later in the JSP, a call to  https://api.dropbox.com/1/account/info using the same stored access token succeeds.

I can even do

wget -O file.out -dvcS --header="Authorization: Bearer  $TOKEN$" " https://api.dropbox.com/1/metadata/auto"

on the command line with the same token gotten by shell access from the database it is stored in and get back an HTTP 200 response with the expected JSON data.

I have yet to rewrite the method using HTTP calls

I am confident that I have ruled out accidental token alteration in that the only wait it fails, ever, is when being used with the Dropbox Java SDK while concurrently being successfully used to make HTTP REST API calls.

Greg-DB
Dropbox Staff

Right, since the token works with the HTTP interface and not the Java SDK, perhaps it is getting corrupted while being passed to the SDK. That is, I'd try adding some logging to make sure dla.getOAuth2AccessToken().toString() is working and returning the right thing. 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Cory A. New member | Level 1
What do Dropbox user levels mean?