OAuth 回调 URI 的 Android 深层链接不起作用

问题描述

我正在尝试在一个非常基本的 Android 应用程序中使用隐式深层链接。目标是使用 Strava 的 OAuth 服务器演示基本的 OAuth 流程,以访问其 API。

Strava 会自动将“localhost”以及您在注册“应用程序”时输入的任何授权回调域列入白名单。我依赖本地主机。

我有一个活动和一些片段。在 Android Studio 中,我添加一个指向非初始片段的深层链接。我的初始片段要求输入登录名和密码,然后创建一个 IntentUri 以访问 Strava OAuth 服务器。这一切都很好,它在 OAuth 请求中与授权请求一起传递回调 URI。问题是回调URI没有去到深层链接

我尝试了几种回调 URI 组合:

  1. http://localhost/fibonacci/itemFragment
  2. localhost/fibonacci/itemFragment
  3. myapp://localhost/fibonacci/itemFragment

这些都不起作用(是的,我总是同时更新 OAuth 请求和 xml 中描述深层链接的 URI。

  1. 回调 URI 触发打开浏览器的请求。
  2. Strava 站点将回调 uri 标记为无效。
  3. 回调似乎没有发生。

我也尝试通过创建快捷方式来测试这一点,但在每个浏览器中都会尝试打开快捷方式。
下面是我的 android 清单文件、我的快捷方式.xml 文件和我的 nav_graph.xml。

即使 Strava 因任何原因无法工作,我也至少应该能够使用快捷方式。

感谢任何帮助。

-------------------AndroidManifest.xml------------------------- --------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fibonacci">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Fibonacci">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.Fibonacci.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <Meta-data android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>
    </application>

</manifest>

----------------------shortcuts.xml-------------- ---------------

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="RichShortcut"
        android:enabled="true"
        android:shortcutShortLabel="@string/static_shortcut_label_short"
        android:shortcutLongLabel="@string/static_shortcut_label_long"
        android:shortcutdisabledMessage=
            "@string/static_shortcut_disabled_message">
        <!--        android:icon="@drawable/donut_with_sprinkles">-->
        <intent
            android:action="android.intent.action.VIEW"
            android:data="myapp://localhost/fibonacci/itemFragment" />
    </shortcut>
</shortcuts>

--------------------------nav_graph.xml------------------ -------------------

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.example.fibonacci.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first">

        <action
            android:id="@+id/action_FirstFragment_to_SecondFragment"
            app:destination="@id/SecondFragment" />
    </fragment>
    <fragment
        android:id="@+id/SecondFragment"
        android:name="com.example.fibonacci.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second">

        <action
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />
    </fragment>
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.fibonacci.ui.login.LoginFragment"
        android:label="fragment_login"
        tools:layout="@layout/fragment_login" />
    <fragment
        android:id="@+id/itemFragment"
        android:name="com.example.fibonacci.ItemFragment"
        android:label="fragment_item_list"
        tools:layout="@layout/fragment_item_list">
        <deepLink
            android:id="@+id/deepLink"
            app:uri="myapp://localhost/fibonacci/itemFragment" />
    </fragment>
</navigation>

解决方法

您尚未在清单的导航图中注册您的 <deepLink>,因此它永远不会被 Android 操作系统触发。

根据implicit deep link documentation

要启用隐式深层链接,您还必须对应用的 manifest.xml 文件进行添加。将单个 <nav-graph> 元素添加到指向现有导航图的 Activity,如以下示例所示:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/Theme.Fibonacci.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <meta-data android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts" />

    <!-- This line is what adds the correct intent filter for your deepLink -->
    <nav-graph android:value="@navigation/nav_graph" />
</activity>