capacitor-community / generic-oauth2

Generic Capacitor OAuth 2 client plugin. Stop the war in Ukraine!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HELP: Getting "USER_CANCELLED" error with Google oAuth2 in Android Simulator

jordanranson opened this issue · comments

Description

Hello, thanks for taking the time to look at my request. I'm not a mobile developer and have been banging my head against the wall for a few days now trying to figure out why I would get a USER_CANCELLED error when the oAuth flow appears to be working in the app. I'd appreciate any hints or advice on what I can do to get this working. I even followed the additional instructions for USER_CANCELLED issues that are mentioned here (https://github.com/moberwasserlechner/capacitor-oauth2#android-1), but no luck (I am not using Azure).

Thanks!


Issue:

Getting "USER_CANCELLED" error with Google oAuth2 in Android Simulator but everything seems to be setup correctly and the oAuth flow successfully completes in the simulator.

Example flow:

  1. Click Google login button, which triggers OAuth2Client.authenticate
  2. Google oAuth login dialog appears. Sign in successfully, dialog closes.
  3. Return to app, USER_CANCELLED error coming from rejected promise in JavaScript/LogCat console. No additional details. I do not have access to oAuth logs on Google's side at the moment.

LogCat output:

D/Capacitor: App started
D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 1765555632
D/Capacitor: Sending plugin error: {"save":false,"callbackId":"25800850","pluginId":"OAuth2Client","methodName":"authenticate","success":false,"error":{"message":"USER_CANCELLED"}}
D/Capacitor: App resumed

JS Console output:

result OAuth2Client.authenticate (#104032769)
{message: "USER_CANCELLED"}

Capacitor version:

Run npx cap doctor:

Latest Dependencies:

@capacitor/cli: 4.6.2
@capacitor/core: 4.6.2
@capacitor/android: 4.6.2
@capacitor/ios: 4.6.2

Installed Dependencies:

@capacitor/core: 3.4.3
@capacitor/ios: 3.4.3
@capacitor/cli: 3.4.3
@capacitor/android: 3.4.3


### Library version:
<!-- Please remove all items that are not relevant. -->

- 3.0.1

### OAuth Provider:
<!-- Please remove all items that are not relevant. -->

- Google

### Your Plugin Configuration
<!-- Without secret stuff (of course). -->

```typescript
// capacitor.config.json
{
  "appId": "com.addictinggames.starveio",
  "appName": "client",
  "webDir": "www",
  "bundledWebRuntime": false,
  "server": {
    "hostname": "capacitor.starve.io",
    "androidScheme": "https"
  },
  "plugins": {
    "CapacitorCookies": {
      "enabled": true
    }
  }
}

// OAuth2Client.authenticate options
{
        authorizationBaseUrl: "https://accounts.google.com/o/oauth2/auth",
        accessTokenEndpoint: "https://www.googleapis.com/oauth2/v4/token",
        scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
        resourceUrl: "https://www.googleapis.com/userinfo/v2/me",
        logsEnabled: true,
        web: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "token", // implicit flow
            accessTokenEndpoint: "", // clear the tokenEndpoint as we know that implicit flow gets the accessToken from the authorizationRequest
            redirectUrl: "http://localhost:8080",
            windowOptions: "height=600,left=0,top=0"
        },
        android: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "code", // if you configured a android app in google dev console the value must be "code"
            redirectUrl: "com.addictinggames.starveio:/" // package name from google dev console
        },
        ios: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "code", // if you configured a ios app in google dev console the value must be "code"
            redirectUrl: "com.starveio.dev:/" // Bundle ID from google dev console
        }
    }

Affected Platform(s):

  • Android
    • Version/API Level: 32
    • Device Model: Pixel 4/Pixel 5
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.addictinggames.starve">

    <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/AppTheme"
    >
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/admob_app_id"
        />
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        
        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.addictinggames.starve.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
        >
            <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="@string/custom_url_scheme" android:host="oauth" />
            </intent-filter>
        </activity>

        <activity 
            android:name="net.openid.appauth.RedirectUriReceiverActivity" 
            android:exported="true"
        >
            <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="@string/custom_url_scheme" android:host="oauth" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

I faced exactly the same issue with an AppLink domain and ended up making it work by adding the net.openid.appauth.RedirectUriReceiverActivity like you did but I had to remove the original intent-filter from the MainActivity. Hope that helps!