1

我是 Android 开发新手,我正在使用 Eclipse 开发 Android 应用程序。我提供了在 Dropbox 上同步数据库的功能。为此,Dropbox 给了我一个用于身份验证的键值。此密钥必须插入 AndroidManifest.xml

<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask" >
<intent-filter>
    <!-- Change this to be db- followed by your app key -->
    <data android:scheme="db-xxxxxxxxxx" />

    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

但是使用这种逻辑,最终用户将无法更改此值以同步他的保管箱帐户上的数据库,而不是我的。我已经制作了一个首选项屏幕来将密钥存储在应用程序首选项中,但是我没有找到从 Android Manifest 读取值的代码。我想它就在这里,但我是新手,我不明白如何编辑我的代码:

 public void startAuthentication(Context context) {
    AppKeyPair appKeyPair = getAppKeyPair();

    // Check if the app has set up its manifest properly.
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    String scheme = "db-" + appKeyPair.key;
    String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
    testIntent.setData(Uri.parse(uri));
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(testIntent, 0);

    if (0 == activities.size()) {
        throw new IllegalStateException("URI scheme in your app's " +
                "manifest is not set up correctly. You should have a " +
                "com.dropbox.client2.android.AuthActivity with the " +
                "scheme: " + scheme);
    } else if (activities.size() > 1) {
        // Check to make sure there's no other app with this scheme.
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Security alert");
        builder.setMessage("Another app on your phone may be trying to " +
                "pose as the app you are currently using. The malicious " +
                "app cannot access your account, but linking to Dropbox " +
                "has been disabled as a precaution. Please contact " +
                "support@dropbox.com.");
        builder.setPositiveButton("OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
        return;
    } else {
        // Just one activity registered for the URI scheme. Now make sure
        // it's within the same package so when we return from web auth
        // we're going back to this app and not some other app.
        String authPackage = activities.get(0).activityInfo.packageName;
        if (!context.getPackageName().equals(authPackage)) {
            throw new IllegalStateException("There must be an " +
                    "AuthActivity within your app's package registered " +
                    "for your URI scheme (" + scheme + "). However, it " +
                    "appears that an activity in a different package is " +
                    "registered for that scheme instead. If you have " +
                    "multiple apps that all want to use the same access" +
                    "token pair, designate one of them to do " +
                    "authentication and have the other apps launch it " +
                    "and then retrieve the token pair from it.");
        }
    }

    // Start Dropbox auth activity.
    Intent intent = new Intent(context, AuthActivity.class);
    intent.putExtra(AuthActivity.EXTRA_INTERNAL_CONSUMER_KEY,
            appKeyPair.key);
    intent.putExtra(AuthActivity.EXTRA_INTERNAL_CONSUMER_SECRET,
            appKeyPair.secret);
    if (!(context instanceof Activity)) {
        // If starting the intent outside of an Activity, must include
        // this. See startActivity(). Otherwise, we prefer to stay in
        // the same task.
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    context.startActivity(intent);

你能帮助我吗?

先感谢您

4

2 回答 2

1

我认为您可能误解了该密钥的用途。它不用于选择与哪个帐户同步文件。

您要更改的密钥是 APP API 密钥。每个用户都不需要唯一的密钥,除非您的目标是已经拥有自己的 API 密钥的开发人员。这用于识别您的应用并将其关闭(除其他外),如果它给 Dropbox 造成问题。

您尝试编辑的此密钥的特定实例是在用户使用其个人帐户进行身份验证后将控制权返回给您的应用程序。这需要与您在调用 AppKeyPair 时用于身份验证的密钥保持同步 appKeys = new AppKeyPair(APP_KEY, APP_SECRET);

您将无法在运行时修改该意图过滤器中的数据标记。

于 2013-01-18T10:52:18.410 回答
0

对象 appKeyPair.key 包含您想要的密钥。我不知道是什么AppKeyPair appKeyPair = getAppKeyPair();,但我认为您可以将其删除并从共享首选项中放置一个简单的字符串,而不是调用appKeyPair.key.

于 2013-01-18T10:46:03.573 回答