FOSOAuthServerBundle: Invalid grant_type parameter or parameter missing

风流意气都作罢 提交于 2019-12-25 04:26:59

问题


I'm trying to build a REST API using Symfony 3.1 and the FOSRestBundle, FOSUserBundle and FOSOAuthServerBundle. I managed to achieve this following the guide at https://gist.github.com/tjamps/11d617a4b318d65ca583.

I'm now struggling at the authentication process. When I make a POST request to the server for authentication (to localhost:8000/oauth/v2/token) with the parameters encoded in json in the request body:

{
    "grant_type": "password",
    "client_id": "1_myveryverysecretkey",
    "client_secret": "myveryverymostsecretkey",
    "username": "theuser",
    "password": "thepassword"
}

The additional HTTP Headers are the following:

Accept: application/json
Cache-Control: no-store, private
Connection: close
Content-Type: */json

The client in the db table oauth2_client has the "password" grant_type a:1:{i:0;s:8:"password";}, as suggested by the guide.

The server is accepting the request, but I always get the response

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

Any suggestions what I am missing? Thanks!


回答1:


I had the same problem. It seems fosOAuthBundle is not accepting json. if you send the query with form fields, it will work.




回答2:


This is because FOSRestBundle uses a body listener which converts underscored keys to camel case. So the parameters that your OAuth2 server gets are not grant_type, but rather grantType, which it cannot process and so it gives you that error.

A solution for this would be to use a custom array normalizer on the body listener of fos rest.




回答3:


really the FOSRestBundle Body Listener is the main 'cause' of this issue.

Array normalizer config

fos_rest:
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys 

it converts _ to camel case format.

The solution was remove it of my configuration by the moment.

calling again /oauth/v2/token endpoint:

{
    "access_token": "NDBlZGViN2YwZGM5MTQ3ZTgwN2FhOGY4MDU4MTc1MTY2YzZmOThlMTdkM2JiZDJmMDVmNTg3MjU4N2JmODY3ZA",
    "expires_in": 3600,
    "token_type": "bearer",
    "scope": null,
    "refresh_token": "MDRiODllNjlhZWYxZjI5MjlhMzAxNGVhMDY5NjQxMmVmNDE5MzY3YzU0MGM0MDU1ZTVlY2Y2Zjg4ZTYyYzU3Mw"
}


来源:https://stackoverflow.com/questions/40436958/fosoauthserverbundle-invalid-grant-type-parameter-or-parameter-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!