简体   繁体   中英

React native OAuth2 redirect_uri: Invalid scheme

recently I've been learning about react native and I encountered a so farm unsolvable problem. I'm trying to authenticate using gmail OAuth2 from my app, as a redirect parameter I put myscheme://oauth the whole url looks like this:

https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id=XXXXXXXX&redirect_uri=myscheme://oauth&scope=https://www.googleapis.com/auth/spreadsheets

I registered an intent-filter in my manifest.xml which looks like this:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="myscheme" android:host="oauth"/>
        </intent-filter>
      </activity>

I am using react native Linking to open the URL and the error I get (in the browser) is 400 Invalid parameter value for redirect_uri: Invalid scheme: myscheme://oauth

Can anyone help me what am I missing here?

From the docs , I see two problems with your URL :

  1. The custom scheme must contain a period to be valid

Change myscheme to something like com.myapp .

  1. Note the single "/" after the custom URI scheme, which is different from regular HTTP URLs

Change myscheme://oauth... to myscheme:/oauth...

After those correction, your URL should look like com.myapp.myscheme:/oauth... instead of myscheme://oauth...

I'm using AppAuth library to get the OAuth2 tokens. As per the Google's official document , this format has worked for me.

<your.app.application.id>:/oauth2callback

For example, if your app id is com.example.app , then in your AndroidManifest.xml

<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="com.example.app" />
        </intent-filter>
</activity>

And the redirectUrl to be provided to AppAuth would look like this

private val redirectUrl = "com.example.app:/oauth2callback"

Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM