Is there anyway to provide authorization automatically ? The usefulness of providing an API to interact with the data store in Dropbox and requiring a manual authorization is zero.
What exactly do you mean by "provide authorization automatically?" You certainly can't access a user's Dropbox without that user's permission.
The Dropbox API uses OAuth to get a user's permission. Once the user has authorized your app, you can access their Dropbox. Each user of your app should only need to authorize the app once.
So that clarifies things a bit. The issue is that it is my drop box and I wanted API access to it. I was not sure if I had to CONTINUOUSLY allow (which made no sense) and thus it would be crazy to go that route.
Once I approve access and to that app, then it would work onwards and that makes more sense. However, executing against a Java client is currently the issue, as I have yet to find a good example that automatically retrieves the auth code such as through an HTTP client and avoid manual interaction from the auth point (after auth is provided from the web console).
The tutorial shows how to go through the auth flow in a command-line app (https://www.dropbox.com/developers/core/start/java), and as it says, you can just store the access token for future use:
The access token is all you'll need to make API requests on behalf of this user, so you should store it away for safe-keeping (even though we don't for this tutorial). By storing the access token, you won't need to go through these steps again unless the user reinstalls your app or revokes access via the Dropbox website.
If you just need an access token for your own account—no other user will use your app—you can generate an access token directly from the app console: https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/.
I did use that example and stored the code from the dropbox site into the app and ran it again:
// String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim(); String code = "cQNNj44-oxgAAAAAAAAJWMnVOFCIlA4xqXbrz56BZPY";
On the 2nd run, it stated:
Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has already been used", "error": "invalid_grant"}
Thus, you can't use the code again ?
The exception is correct... you're trying to reuse an authorization code that's already been used. An authorization code can only be used once (and within a limited time period).
What you need to do is reuse the access token.
Once you have an access token, your code can just be this:
DbxClient client = new DbxClient(config, "<YOUR ACCESS TOKEN HERE>");
Updated the test client as you showed:
and it worked. But, you stated:
"An authorization code can only be used once (and within a limited time period)." .... does that mean that this auth code will become invalidated in the future ?
"Authorization code" and "access token" are two different things.
You were trying to reuse an authorization code, which won't work.
Now you're reusing an access token, which will work. (Access tokens are meant to be reused indefinitely.)