My app just needs to upload a backup file to a dropbox account from mine, so the access must be off-line (no prompting to the user, that would be confusing). This is carried out when the user finishes inputting data. I previously used the API with the following code (Java SDK):
DbxRequestConfig config = DbxRequestConfig.newBuilder( getPackageName() ).build();
DbxClientV2 client = new DbxClientV2( config, getString( R.string.dropbox_token ) );
client.files()
.uploadBuilder( toPath )
.withMode( WriteMode.OVERWRITE )
.uploadAndFinish( in );
But now this does not work anymore, or better speaking, it works but the token expires, so it only works once. I've looked the docs for the Java SDK, but there doesn't seem to be a simple example for offline access.
What I tried so far:
- I obtained an access token, that allows the initial authentication.
- I substituted the generated short-lived token with an access token. But this does not work by itself.
- There is a promising method in the client that refreshes a token, so I tried calling it and creating a new client, before uploading the file:
try {
DbxRefreshResult result = client.refreshAccessToken();
DbxCredential credentials = new DbxCredential(
getString( R.string.dropbox_token ),
Long.MAX_VALUE,
result.getAccessToken(),
getString( R.string.dropbox_key ),
getString( R.string.dropbox_secret ) );
client = new DbxClientV2( config, credentials );
} catch (DbxException e) {
System.err.println( "Error refreshing token: " + e.getMessage());
}
The refreshing apparently works (no errors, at least), but then the upload fails with a new error: invalid_access_token.
So, should I create a new client with the refresh token or not? Am I refreshing the token correctly?