Authentication on Android

I am trying to authenticate in android but I get an error

"error":"RESTEASY003065: Cannot consume content type"

this is the complete log of the request?

I/okhttp.OkHttpClient: --> POST http://10.0.2.2:7030/auth/realms/retail/protocol/openid-connect/token
I/okhttp.OkHttpClient: Content-Type: application/json; charset=UTF-8
I/okhttp.OkHttpClient: Content-Length: 191
I/okhttp.OkHttpClient: {"client_id":"login-mobile","grant_type":"password","client_secret":"e39159b8-00c9-4a13-aea4-e220b925ed06","scope":"openid profile email offline_access","username":"test","password":"123456"}
I/okhttp.OkHttpClient: --> END POST (191-byte body)
I/okhttp.OkHttpClient: <-- 415 Unsupported Media Type http://10.0.2.2:7030/auth/realms/retail/protocol/openid-connect/token (25ms)
2021-09-06 21:12:27.437 9155-9198/com.gnbit.keeper.shoplift I/okhttp.OkHttpClient: X-XSS-Protection: 1; mode=block
I/okhttp.OkHttpClient: X-Frame-Options: SAMEORIGIN
I/okhttp.OkHttpClient: Referrer-Policy: no-referrer
I/okhttp.OkHttpClient: Date: Tue, 07 Sep 2021 02:12:28 GMT
I/okhttp.OkHttpClient: Connection: keep-alive
I/okhttp.OkHttpClient: Strict-Transport-Security: max-age=31536000; includeSubDomains
I/okhttp.OkHttpClient: X-Content-Type-Options: nosniff
I/okhttp.OkHttpClient: Content-Type: application/json
I/okhttp.OkHttpClient: Content-Length: 55
I/okhttp.OkHttpClient: {"error":"RESTEASY003065: Cannot consume content type"}
I/okhttp.OkHttpClient: <-- END HTTP (55-byte body)

what could be wrong?

You’re not sending application/json in that request. The header should be Content-Type: application/x-www-form-urlencoded.

As an aside, it’s recommended not to use password grant type. If you are securing an Android application, try OpenID AppAuth: GitHub - openid/AppAuth-Android: Android client SDK for communicating with OAuth 2.0 and OpenID Connect providers.

2 Likes