Symfony2 FOSOAuthServerBundle授予类型密码需要客户端机密


Symfony2 FOSOAuthServerBundle grant type password requires client secret

我正在集成FOSOAuthServerBundle来处理从移动应用程序到Symfony2后台的登录。我遵循了这个答案的说明,但由于我以前从未使用过OAuth2,有时我会有点不知所措。

我尝试使用"密码"grant_type登录,但由于某种原因,除非我将client_secret指定为GET参数,否则无法登录。我真的应该这么做吗?

以下是我的请求:

http://myserv.local/app_dev.php/oauth/v2/token ?client_id=1_4up4x3hpaask4g0sok0so8sg00gk48c44cc0wkwwsg8048wcog &grant_type=password &username=test@test.com &password=somepassword

除非添加client_secret参数,否则返回此响应:

{"error":"invalid_client","error_description":"The client credentials are invalid"}

是的,您应该包括客户端机密。一旦您提出此请求,您将获得一个access_token,可以用于未来的每个请求,这样您就不需要再次使用凭据或客户端信息,直到access_toke到期。当令牌过期时,即使在那时您也不需要再次使用用户凭据,您也可以使用refresh_token以及客户端id和机密来获得新的access_token。所以你最初的请求应该是这样的:

http://localhost/oauth/v2/token?client_id=[CLIENT_ID]&client_secret=[SECRET]&grant_type=password&username=[USERNAME]&password=[PASSWORD]

在响应中,您将获得access_token,它可以这样使用:

http://localhost/api/users?access_token=[ACCESS_TOKEN]

希望这能为你澄清更多。

当您使用唯一允许的授予类型"password"创建新客户端时,客户端机密是公共的,没有人能够将其与client_credential授予一起使用,这不应该是安全问题。