openid / AppAuth-Android

Android client SDK for communicating with OAuth 2.0 and OpenID Connect providers.
https://openid.github.io/AppAuth-Android
Apache License 2.0
2.89k stars 891 forks source link

File not found exception when requesting token #436

Closed karunanaik closed 5 years ago

karunanaik commented 5 years ago

I am using the google app auth example. I modified the urls as per my oauth provider. I have specified the a valid authorization endpoint, client id, redirect uri, token uri, scope(openid, profile, email), response type (Code). I don't know if there is a way to add consumer secret. I have not seen anywhere in the sample code a way to add consumer secret. When I run the app, I see our sso page, I am able to login, redirect happens. When the request to get token gets executed, I get an exception. The token request is failing. Any idea why this would happen? The token exchange url is correct and I am not sure why it is complaining about file not being found at that particular token url. Any pointers on digging more into this issue?

W/AppAuthSample: Token Exchange failed
    AuthorizationException: {"type":0,"code":3,"errorDescription":"Network error"}
        at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:244)
        at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:206)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.io.FileNotFoundException: <token_url>
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
        at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:239)
        at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:206) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
karunanaik commented 5 years ago

I updated the demo app to using android x. I am also using the latest library version. The build is fine. I am able to see the sso page. I can login but the redirect does not happen as expected. This is how my build.gradle looks like. apply plugin: 'com.android.application'


android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.google.codelabs.appauth"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        android.defaultConfig.manifestPlaceholders = [
                'appAuthRedirectScheme': 'mdconnect'
        ]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'net.openid:appauth:0.7.1'
}

This is a snippet of my manifest.xml

<manifest package="com.google.codelabs.appauth"
          xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="mdconnect.HANDLE_AUTHORIZATION_RESPONSE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <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="mdconnect"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
iainmcgin commented 5 years ago

You'll get a FileNotFoundException if the server responds with a 404 response code for your token URL. Please check again that you have specified the right token endpoint for the IDP you are integrating with.

Johnnie843 commented 5 years ago

Im getting the same error but I'm using my own keycloak server. I get the SSO login screen but when it does the callback I get:

06-17 02:15:42.500 26098-26098/com.google.codelabs.appauth W/AppAuthSample: Token Exchange failed AuthorizationException: {"type":0,"code":3,"errorDescription":"Network error"} at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:244) at net.openid.appauth.AuthorizationService$TokenRequestTask.doInBackground(AuthorizationService.java:206) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

I check the Auth and Token URI and they are correct and on the keycloak server I see I have active session with my login.

Johnnie843 commented 5 years ago

@iainmcgin

essieM commented 4 years ago

@Johnnie843 were you able to resolve this issue? I'm also getting the same error with my Keycloak server.

hb0 commented 1 year ago

Just for future readers: FileNotFoundException also occurs when client_id is not set in the client.

(When using Keycloak)