0

I've got InvalidAccessTokenException at any attempt to upload file to Dropbox.

For authorization I use next algorithm:

Auth.startOAuth2Authentication(context, APP_KEY);

//granting access
//previous activity is reopened

String accessToken = Auth.getOAuth2Token();
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(CLIENT_ID)
                    .withHttpRequestor(OkHttp3Requestor.INSTANCE)
                    .build();
DbxClientV2 client = new DbxClientV2(requestConfig, accessToken);
client.files().uploadBuilder(ADDRESS_IN_DROPBOX)
                            .uploadAndFinish(inputStream);

which returns InvalidAccessTokenException.

Maybe I'm doing something wrong.

Also maybe Auth.getOAuth2Token() return auth code instead of access code (which is a bit obvious from method name), but how to get access code when auth code is known?

PS I've used Android project as example - https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android and performed auth exactly as in example.

EDIT-----------------------------------------------------------------------------------------------

Problem occurs ONLY after revoking access to the app from account settings at Dropbox webpage.

1

2 Answers 2

1

You noted:

Problem occurs ONLY after revoking access to the app from account settings at Dropbox webpage.

In that case, this InvalidAccessTokenException is expected. If a user revokes the token, e.g., by unlinking the app via https://www.dropbox.com/account/security , any further API calls attempted with that token will fail with this exception. You should have your code catch this exception and prompt the user to re-link the app if they want to continue using it.

1
  • I've done so. Catched exception and started authorization process again. But after attempt to upload file right after authorization there are another InvalidAccessTokenException. Commented Jun 30, 2016 at 6:39
1

I found problem in my code. I've used DropboxClientFactory like in example from Dropbox where client init looks like this

public static void init(String accessToken) {
    if (sDbxClient == null)) {
        DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("ID")
                .withHttpRequestor(OkHttp3Requestor.INSTANCE)
                .build();

        sDbxClient = new DbxClientV2(requestConfig, accessToken);

    }
}

And when access token was revoked - init process was not called because sDbxClient is already initialized.

So I added variable accessCode to DropboxClientFactory to hold used access token and now init looks like this

public static void init(String accessToken) {
        if (sDbxClient == null || !accessToken.equals(accessCode)) {
            DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("ID")
                    .withHttpRequestor(OkHttp3Requestor.INSTANCE)
                    .build();

            sDbxClient = new DbxClientV2(requestConfig, accessToken);

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.