Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't authenticate on Android using AppAuth; web view never closes.

ANSWERED

Hi - 

My app allows the user to authenticate with the Fitbit Web API so we can access the user's step count.  I am using AppAuth for this on both iOS and Android.

Recently this stopped working on Android, and I'm not sure why.  The Fitbit OAuth web interface appears, but when the user taps either "Allow" or "Deny", it doesn't dismiss.  Instead, it appears to refresh, and now there are two instances of my app in the Android app list. 

Here's the code:

private fun authorize()
{
    val authEndpoint = Uri.parse(config.FITBIT_AUTH_URL)
    val tokenEndpoint = Uri.parse(config.FITBIT_TOKEN_URL)
    val redirectUrl = Uri.parse(config.FITBIT_REDIRECT_URL)

    val authConfig = AuthorizationServiceConfiguration(authEndpoint, tokenEndpoint)

    val request = AuthorizationRequest.Builder(authConfig, config.FITBIT_CLIENT_ID, ResponseTypeValues.CODE, redirectUrl)
                .setPrompt("consent")
                .setScope(config.FITBIT_SCOPES.joinToString((" ")))
                .build()
    val authIntent = fitbitAuthService.getAuthorizationRequestIntent(request)
    this.authLauncher.launch(authIntent)
}

val authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        // THIS NEVER GETS CALLED
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            if (data != null) {
                val authResponse = AuthorizationResponse.fromIntent(data)
                val authException = AuthorizationException.fromIntent(data)
                // success
            }
        } else {
            // canceled       
        }
    }

in AndroidManifest.xml:

        <activity
            android:name="net.openid.appauth.RedirectUriReceiverActivity"
            android:exported="true"
            tools:node="replace">
            <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.mycompany.myapp" android:host="fitbit-auth"/>
            </intent-filter>
        </activity>

All this works fine on iOS using the corresponding AppAuth library.

Any ideas?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I don't understand why, but removing the task affinity from the launch activity (which is not the activity that calls the above code) fixed this.

View best answer in original post

Best Answer
0 Votes
1 REPLY 1

I don't understand why, but removing the task affinity from the launch activity (which is not the activity that calls the above code) fixed this.

Best Answer
0 Votes