0

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

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

{
    "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

正如指南所建议的,数据库表 oauth2_client 中的客户端具有“密码” grant_type a:1:{i:0;s:8:"password";}

服务器正在接受请求,但我总是得到响应

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

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

4

3 回答 3

2

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

于 2016-12-05T17:59:56.837 回答
0

这是因为 FOSRestBundle 使用主体侦听器将带下划线的键转换为驼峰式大小写。因此,您的 OAuth2 服务器获取的参数不是grant_type,而是grantType它无法处理的参数,因此它会给您该错误。

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

于 2017-02-02T13:14:27.123 回答
0

实际上,FOSRestBundle Body Listener是这个问题的主要原因。

数组规范化配置

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"
}
于 2018-06-04T02:55:41.747 回答