Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - Android deep link for OAuth callback URI not working

I'm trying to get an implicit deep link working in a very basic Android app. The goal is to demonstrate a basic OAuth flow with Strava's OAuth server to get access to their API.

Strava automatically whitelists 'localhost' plus any authorization callback domain you enter when you register your 'app'. I'm relying on localhost.

I have an activity plus some fragments. In Android Studio I've added a deep link to a fragment that isn't the initial one. My initial fragment asks for a login name and password then creates an IntentUri to hit the Strava OAuth server. This goes just fine and it passes the callback URI in the OAuth request along with the grant requests. The problem is that the callback URI does not go to the deep link.

I've tried a few combinations for callback URIs:

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

None of these work (yes I always update the URI in the OAuth request and in xml that describes the deep link at the same time.

  1. the callback URI triggers a request to open the browser.
  2. the Strava site flags the callback uri as invalid.
  3. the callback doesn't seem to occur.

I've also tried to test this by creating a shortcut but in each the browser tries to open the shortcut.
Below are my android manifest file, my shortcuts.xml file and my nav_graph.xml.

I should at least be able to get the shortcut to work even if Strava wouldn't work for whatever reason.

Any help is appreciated.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You haven't registered your <deepLink> in your navigation graph in your manifest, so it will never be triggered by the Android OS.

As per the implicit deep link documentation:

To enable implicit deep linking, you must also make additions to your app's manifest.xml file. Add a single <nav-graph> element to an activity that points to an existing navigation graph, as shown in the following example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...