0

我正在用网页编写一个 API。我想要 Oauth2 登录,这样我就可以从移动设备保持会话,我已经安装了 GEM 门卫并按照本网站的说明运行迁移。

我被卡住的地方是 resource_owner_from_credentials 部分,因为我有一个用户模型,它具有由 rails 的 has_secure_password 助手提供的身份验证方法。这就是我的 /config/initializers/doorkeeper.rb 文件的样子

Doorkeeper.configure do
  # Change the ORM that doorkeeper will use.
  # Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper
  orm :active_record

  # This block will be called to check whether the resource owner is authenticated or not.
  resource_owner_from_credentials do
    User.find_by_email(params[:email]).authenticate(params[:password])
  end
##lots of comments
end

什么时候去

localhost:3000/oauth/authorize 

我明白了:

config/initializers/doorkeeper.rb:8:in `block (2 levels) in <top (required)>'

然后我尝试了:

 http://127.0.0.1:3000/oauth/authorize?email=puca@gmail.com&password=porche 

和相同的

我在做什么错?我应该如何配置source_owner_authenticator 块?我如何获得令牌?

4

1 回答 1

1

根据此门卫 wiki 页面/oauth/token,您需要使用以下参数向 API 发送 POST 请求:

{
  "grant_type"    : "password",
  "username"      : "user@example.com",
  "password"      : "sekret"
}

处理此请求时,门卫调用该resource_owner_from_credentials块并将参数传递给它。所以你可以访问名为username而不是的参数email

总而言之,将 API 端点修复为/oauth/token,更改params[:email]params[:username],一切都应该正常工作。

于 2015-02-22T16:16:34.327 回答