How do I authenticate in DropboxAPI v2 for Android

2019-02-20 17:32发布

I've just started out and I'm following this tutorial

https://www.dropbox.com/developers/documentation/java#tutorial

But there's no login. Nothing asks you for a username, password. That means I can't actually get a GUI that every app with "Share to Dropbox" option has.

Am I stuck with one account? Do I have to find a way to get a person's ACCESS_TOKEN or is there a more elegant GUI solution out there(like with Google Drive and their intentsenders)?

2条回答
beautiful°
2楼-- · 2019-02-20 17:42

Unfortunately, there isn't much out there in terms of documentation for this flow. Here is how I was able to successfully authenticate users. First, you want to launch the authentication flow with Dropbox's auth activity:

import com.dropbox.core.android.Auth
....
Auth.startOAuth2Authentication(context, context.getString(R.string.dbx_api_app_key))

After a user has successfully authenticated, call the following method in the onResume method of the activity you started the Dropbox activity from:

@Override
public void onResume() {
    super.onResume();
    String token = Auth.getOAuth2Token()
}

The token that you receive here should be used when you create your instance of the DbxClientV2 like so:

DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(MyUtils.getVersionText(context))
                .withHttpRequestor(OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
                .build()

DbxClientV2 dbxClient = DbxClientV2(requestConfig, accessToken)

You'll also need the following dependencies in your build.gradle file:

implementation 'com.squareup.okhttp:okhttp:2.7.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
查看更多
smile是对你的礼貌
3楼-- · 2019-02-20 18:04

To use the Dropbox API v2 in Android, you should use the API v2 Java SDK. There's an example Android app that uses it included with the SDK. You should refer to that as an example of how to implement the app authorization flow, which is accomplished via OAuth 2. That requires the user to authorize your app with Dropbox, by signing in to Dropbox if necessary. After that, your app can store and re-use the resulting access token for that user, as the example does here.

Implementing it that way allows any user to connect their Dropbox account to your app. You can also handle multiple accounts per instance of your app if you want.

查看更多
登录 后发表回答