FOSOAuthServerBundle:缺少grant_type参数或参数无效

时间:2016-11-05 10:07:40

标签: json symfony fosrestbundle fosoauthserverbundle

我正在尝试使用Symfony 3.1和FOSRestBundle,FOSUserBundle和FOSOAuthServerBundle构建REST API。我设法按照https://gist.github.com/tjamps/11d617a4b318d65ca583的指南实现了这一目标。

我现在正在努力进行身份验证过程。当我向服务器发出POST请求以进行身份​​验证(到localhost:8000 / oauth / v2 / token)时,请求正文中的json编码参数:

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

其他HTTP标头如下:

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

db表oauth2_client中的客户端具有“密码”grant_type a:1:{i:0;s:8:"password";},如指南所示。

服务器正在接受请求,但我总是收到回复

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

有什么建议我缺少什么?谢谢!

3 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。似乎fosOAuthBundle不接受json。如果您使用表单字段发送查询,它将起作用。

答案 1 :(得分:0)

这是因为FOSRestBundle使用一个将下划线键转换为camel case的正文监听器。因此,您的OAuth2服务器获取的参数不是grant_type,而是grantType,它无法处理,因此它会为您提供错误。

对此的解决方案是在fos rest的body侦听器上使用自定义数组规范化器。

答案 2 :(得分:0)

FOSRestBundle Body Listener 确实是此问题的主要原因。

Array normalizer config

fos_rest:
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys 

它将_转换为驼峰格式。

解决方案暂时删除了我的配置。

再次呼叫/oauth/v2/token端点:

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