Postman调用Keycloak API遇400错误,提示grant_type参数缺失求助
Fixing "Missing form parameter: grant_type" Error in Keycloak Token API Call
Hey there, let's get this 400 error sorted out for you. The issue here is a common gotcha with OAuth2 password grant flows and how Keycloak expects parameters to be sent.
What's Going Wrong
Even though you included all the required parameters in the URL query string and set the right Content-Type header, Keycloak's token endpoint doesn't accept grant-type parameters via URL query parameters for the password flow. It expects these values to be sent as form data in the request body instead.
Step-by-Step Fix
Here's exactly what to adjust in Postman:
- Use the correct request method: Make sure your request is set to
POST(not GET) — this is required for the password grant flow. - Remove parameters from the URL: Trim your request URL down to just the base endpoint:
http://{hostname}:8080/auth/realms/master/protocol/openid-connect/token - Add parameters to the request body:
- Switch to the "Body" tab in Postman
- Select the
x-www-form-urlencodedoption (Postman will automatically set the correctContent-Typeheader for you, so you don't need to manually add it) - Add these key-value pairs one by one:
username: adminpassword: adminclient_id: admin-cligrant_type: password
- Send the request: You should now get a valid token response instead of the 400 error.
Extra Checks (If It Still Fails)
- If you're using Keycloak 17 or newer, note that the
/authprefix was removed from the API path. Your URL should be:http://{hostname}:8080/realms/master/protocol/openid-connect/token - Verify that the
adminuser in themasterrealm has the correct password, and that theadmin-cliclient is enabled in Keycloak's admin console. - Double-check that Postman isn't sending any conflicting custom headers that might interfere with the request.
内容的提问来源于stack exchange,提问作者Programmer
相关产品推荐
相关产品推荐

