Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
Robert S.138
9 years agoHelpful | Level 7
AccessToken Error recovery in Auth2 (Android)
In examining the Android sample for Core API v2, I was wondering how to properly handle error recovery in the use of AccessToken. In the example, the AccessToken is returned by Auth.getOAuth2Token()...
Karl R.2
9 years agoNew member | Level 1
Hi Robert,
I will try to answer all your questions. Let me know if I missed one:
Invalid Access Tokens
Whenever you issue a request using the SDK, your request may fail because your access token is no longer valid. This can happen because the token expired (very rare) or the user has revoked access to your app. The SDK will raise an InvalidAccessTokenException exception when this happens. For example:
try {
ListFolderResult result = sDbxClient.files().listFolder(");
} catch (InvalidAccessTokenException ex) {
// ... handle invalid token here. Probably ask user to re-authorize app
} catch (DbxException ex) {
// ... handle all other failures
}
Note that InvalidAccessTokenException is a checked exception and is not thrown when creating a client (e.g. when calling new DbxClientV2(requestConfig, accessToken)). You will have to issue a request to determine whether the token is valid or not.
Handling DbxClientV2 Instances
The android example uses a static DbxClientV2 instances for all requests. This is safe and should not cause any problems since the client is a thin, thread-safe wrapper around your HTTP client. There are no resources that are held by the client. So you can create them as many times as you want or use a singleton pattern.
You must, however, be careful with your HTTP client. The Dropbox SDK can either use the native Java network stack or an HTTP client of your choosing. The android example uses the OkHtttp3 client for issuing requests. These clients are generally only created once for an app and shared for all network requests. A common practice is to have an HTTP client with a configured connection pool that is shared across your app. In this case, every time you create an SDK client, you would want to pass in the same instance of your HTTP client.
Below is an example of how to share an OkHttp3 client across your app and the Dropbox SDK:
OkHttpClient httpClient = OkHttpClient.newBuilder()
.connectionPool(new ConnectionPool(8, 30, TimeUnit.SECONDS))
.build();
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("MyTestApp/1.0")
.withHttpRequestor(new OkHttp3Requestor(httpClient))
.build();
DbxClientV2 dbxClient = new DbxClient(requestConfig, accessToken);
// ... issue Dropbox requests with dbxClient
// ... issue other requests with httpClient
Let me know if you have any other concerns.
About Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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, Facebook or Instagram.
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!