首页 > 解决方案 > 重定向 Uri 未直接在 OAuth2 中打开 Android 应用程序 - 已配置深层链接

问题描述

要求:打开一个 URL 并将响应返回给应用程序并将用户重定向回我的应用程序,因为后面还有其他 API 调用。

<intent-filter
            android:autoVerify="true"
            android:order="1">
            <action android:name="android.intent.action.VIEW" />

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

            <data
                android:host="ab.bc.cd"
               android:port="11001"/>
               <data android:scheme="https" />
                <data android:scheme="http" />
               <data android:pathPrefix="/abc.html" />
        </intent-filter>
    </activity>

在浏览器中打开第一个 api 调用的代码:

if (!TextUtils.isEmpty(url)) {
        final CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
        CustomTabActivityHelper.openCustomTab(MainActivity.this, customTabsIntent, Uri.parse(url), new WebviewFallback() {
            @Override
            public void openUri(Activity activity, Uri uri) {
                    setupCustomTabs(uri);
            }
        });

在这里处理 Intent 部分

  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    final String schemeName = intent.getScheme();
    final Uri data = intent.getData();
    if (data != null && data.getHost() != null && data.getHost().equalsIgnoreCase("")) {
        return;
    }
    if (!TextUtils.isEmpty(schemeName) && schemeName.equalsIgnoreCase(UAEPassConstant.SCHEME)) {
        final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (fragment != null && !(fragment instanceof DisplayFragment)) {
            setFragment(new DisplayFragment(), null, false);
        }
        final String encodedQuery = intent.getData().getEncodedQuery();
        if (!TextUtils.isEmpty(encodedQuery) && !encodedQuery.contains(UAEPassConstant.ERROR_DESCRIPTION)) {
            final String[] queryDataArray = encodedQuery.split("=|&"); // this is to fetch a code value which comes as a result of the first api call
            final LoginApiCall uaePassCall = new LoginApiCall(MainActivity.this, queryDataArray[1], new IUAEPassCallBack() {
                @Override
                public void onSuccessLogin(String userInfoJson, String tokenJson) {
                    Log.e("userInfo", userInfoJson);
                    Log.e("tokenJson", tokenJson);
                    final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
                    if (fragment instanceof DisplayFragment) {
                        ((DisplayFragment) fragment).LoggingData(userInfoJson);

                    }
                }

会发生什么:当我运行此代码时,一切正常,但是当第一个 API 调用链接在浏览器中打开并且我批准它的登录请求时,它会在浏览器本身中显示重定向 URI,并且控件不会直接返回到我的应用程序。相反,它会冻结,然后我需要单击“在 chrome 中打开”(附加),然后它会提示我是否要打开应用程序并选择控件返回到我的应用程序。任何指针将不胜感激。提前致谢!编辑:代码在第一次执行期间正常工作,但第二次出现此问题。

标签: androiddeep-linking

解决方案


我做了很多事情,但最后当我将打开自定义选项卡的代码更改为:

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();

            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(this, Uri.parse(url));

现在,它工作正常。

注意:仅此一项不起作用。我已经检查了几乎所有与此问题相关的可能链接,但事实证明,我使用的深层链接是错误的。请确保您正确设置了方案和主机。我花了 4 天的时间来弄清楚这一点。


推荐阅读