【问题标题】:How to open a custom scheme in android with java如何使用java在android中打开自定义方案
【发布时间】:2020-09-01 04:43:42
【问题描述】:

我正在使用这个 OAuth 库 https://github.com/openid/AppAuth-Android 我已经注册了一个自定义方案

    <activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="kronos"
                  android:host="oauth2"/> <!-- Redirect URI scheme -->
        </intent-filter>
    </activity>

我有这些方法:

private void authorize() {

    AuthorizationRequest authRequest = new AuthorizationRequest.Builder(
            mServiceConfiguration,
            "...", // Client ID
            ResponseTypeValues.CODE,
            Uri.parse("kronos://oauth2/callback") // Redirect URI
    ).setScope("auth").build();


    AuthorizationService service = new AuthorizationService(this);

    Intent intent = service.getAuthorizationRequestIntent(authRequest);
    startActivityForResult(intent, REQUEST_CODE_AUTH);
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_CODE_AUTH) {
        Log.d(TAG, "get the token");
        return;
    }else{
        Log.d(TAG, "wrong code");
        return;
}

它工作正常,但现在我想处理一个自定义 URL,例如“kronos://something”,所以到目前为止这里有 2 个问题:

-可以在onActivityResult中处理吗?如何处理?

-我可以在 chrome 中调用这个自定义协议进行测试吗(就像在带有 safari 的 iOS 中一样)?

编辑:更准确地说,当用户注销以删除 OAuth 令牌时,我收到的是自定义 URL

EDIT2 我已经在清单中添加了它

    <activity android:name=".activity_logoff">
        <intent-filter>
            <data android:scheme="kronos"
                android:host="something"/> <!-- Redirect URI scheme -->
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>

但我不知道如何让应用拦截所有 url "kronos://something"

EDIT3 我现在已经创建了可能需要的 Intent

Uri logoutUri = Uri.parse("kronos://something");
Intent logoutIntent1 = new Intent(Intent.ACTION_VIEW,logoutUri);

EDIT4 我添加了

    logoutIntent1.setPackage(getApplicationContext().getPackageName());

但是接下来要做什么

【问题讨论】:

    标签: java android oauth custom-url-protocol


    【解决方案1】:

    你想要这个kronos://something 做什么?它与身份验证响应有什么关系吗?如果不是,那么 oAuth 与这里无关。

    您即将宣布activity_logoff。您需要做的就是致电: context.startActivity(logoutIntent1) - context 可以是任何 Activity。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2015-10-30
      相关资源
      最近更新 更多