【问题标题】:Implementing resource_owner_authenticator doesnt work实施资源所有者身份验证不起作用
【发布时间】:2014-05-09 22:57:17
【问题描述】:

我正在编写一个带有网页的 API。我想要 Oauth2 登录,这样我就可以在移动设备上保持会话,我已经安装了 GEM 门卫并运行迁移,它是如何在 this site. 上解释的@

我被卡住的地方是 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 块?我如何获得令牌?

【问题讨论】:

    标签: ruby-on-rails-4 oauth-2.0 doorkeeper


    【解决方案1】:

    根据this doorkeeper wiki page,您需要使用以下参数向/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] ,一切都会正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多