【问题标题】:Android deep link for OAuth callback URI not workingOAuth 回调 URI 的 Android 深层链接不起作用
【发布时间】:2021-04-16 00:26:35
【问题描述】:

我正在尝试在一个非常基本的 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 清单文件、我的 shortcuts.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>

【问题讨论】:

    标签: java android oauth-2.0 deep-linking


    【解决方案1】:

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

    根据implicit deep link documentation:

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

    <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>
    

    【讨论】:

    • 啊。 nav_graph 显示在其他地方(抱歉没有包括所有文件)。它在 'content_main.xml' 中描述了这样的片段:app:navGraph="@navigation/nav_graph" /> 我需要你的行加上我的 content_main.xml 中存在的行吗?
    • 你在两个地方都需要它,是的。
    猜你喜欢
    • 2014-11-19
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2020-09-21
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多