Getting access token OAuth2.0

Hi! I’m trying to getting access token in my application, which registered in my development pod. But I keep getting the error - {“error”:“invalid_request”,“error_description”:"‘grant_type’ required."}

I’m sending post request with header “Content_type: application/x-www-form-urlencoded” to “http://1.2.3.4:3000/api/openid_connect/access_tokens” endpoint
Body:
“grant_type=authorization_code&” +
“code=${code}&” +
“redirect_uri=${AppConstant.REDIRECT_URI}&” +
“client_id=${AppConstant.CLIENT_ID}” +
“client_secret=${AppConstant.CLIENT_SECRET}”

I’ve already tried passing grant_type in the request header, tried passing parameters to json, but I always get the same error. All steps up to this point are completed successfully.

Help me please. Sorry for my English.

Hi, and welcome to this community!

While your text-example looks correct, your screenshot shows that you’ve named the field grant_type2, instead of grant_type. Please double-check that you’re using the right name in your code. :slight_smile:

Also, I strongly suggest against writing your own OpenID Connect library, because it can be quite complicated. I’m not quite sure what programming language you’re using in your example, but I’d suggest you check if there is a library for your language!

grant_type2 is incorrect only in the screenshot, tested different variations.
I fixed first problem. The problem for me didn’t become clear, changing the parameters in places, the error became different. {"error":"invalid_client","error_description":"The client identifier provided is invalid, the client failed to authenticate, the client did not include its credentials, provided multiple client credentials, or used unsupported credentials type."}

I use this algorithm - register the application, get the user authentication_code and then get the access_token.

Perhaps I missed something or something I don’t fully understand, tell me, please. Thanks :upside_down_face:

Also provide network request logs

Since you’re using Kotlin, I’ll make a guess that https://github.com/openid/AppAuth-Android should be a great library for you! Here’s how I integrated it for my Flutter app: https://github.com/jhass/insporation/blob/master/android/app/src/main/kotlin/jhass/eu/insporation/AppAuthHandler.kt#L90

As for your own code, it’s hard to debug without being on-hands. I have to admit I sometimes debugged the diaspora side of things to understand an error better while implementing my client. But one thing that’s suspicious is that you just string-concatenate your body together without taking care to properly URL-encode the values, in particular the redirect uri might contain things that break the encoding otherwise.

Thank you very much. After testing this method, I realized that there is a very sensitive parser and I guessed that I had a problem with data presentation.