【问题标题】:Can't retrieve access_token, refresh_token from Omniauth authentication strategy with Devise, Rails 4无法使用 Devise、Rails 4 从 Omniauth 身份验证策略中检索 access_token、refresh_token
【发布时间】:2017-04-26 17:19:00
【问题描述】:

我正在使用这样的宝石:

gem "omniauth-yandex"
gem 'devise'

我的设置。

路线

 devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }

设计初始化器

config.omniauth :yandex, Rails.application.secrets.client_id , Rails.application.secrets.password, callback_url: "http://cb2bcdc4.ngrok.io/users/auth/yandex/callback"

用户模型

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:yandex]


   def self.from_omniauth(auth) 

      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.email =  auth.info.email
        user.password = Devise.friendly_token[0,20]
      end

  end

回调控制器

class CallbacksController < Devise::OmniauthCallbacksController        

    def yandex
       #empty
    end
end

查看:

  <%= link_to "Sign in with Yandex", user_yandex_omniauth_authorize_path, id: "sign_in" %>

当我单击“使用 Yandex 登录”时,我的应用程序会提示用户许可,然后重定向回我的应用程序。用户是在我的数据库中使用这些字段创建的——电子邮件、提供者、uid。但我也想拥有 access_token、refresh_token 和 expires_at,因为我使用的 Yandex API 很少。

当我 httlog'ed 上述操作(从单击“登录 ..”到重定向回我的应用程序)时,我收到了以下结果:

D, [2017-04-26T19:17:42.091838 #24865] DEBUG -- : [0;30;101m[httplog] Connecting: oauth.yandex.ru:443[0m
D, [2017-04-26T19:17:42.266645 #24865] DEBUG -- : [0;30;101m[httplog] Sending: POST http://oauth.yandex.ru:443/token[0m
D, [2017-04-26T19:17:42.267040 #24865] DEBUG -- : [0;30;101m[httplog] Data: client_id=097253682f9f41289ec5exxxxxxx&client_secret=xxxxxxdb4fxx0eadcbb8a4143&code=xxxxx327&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fcb2bcdc4.ngrok.io%2Fusers%2Fauth%2Fyandex%2Fcallback%3Fstate%xxxxxxxxxxx%26code%xxxx[0m
D, [2017-04-26T19:17:42.410712 #24865] DEBUG -- : [0;30;101m[httplog] Status: 200[0m
D, [2017-04-26T19:17:42.410945 #24865] DEBUG -- : [0;30;101m[httplog] Benchmark: 0.143445 seconds[0m
D, [2017-04-26T19:17:42.411168 #24865] DEBUG -- : [0;30;101m[httplog] Response:
{"token_type": "bearer", "access_token": "xxxxxxxxuyBwtcyAFjkBZo3F3MCiIaTI", "expires_in": 31528753, "refresh_token": "xxxxxxxxxxxClSH:Pts0u-Mfls-vdEc7-zTOod9ZWzegNFRxxxxxxxxxxxxxKHpwsqBFUHHKtg"}[0m
D, [2017-04-26T19:17:42.414748 #24865] DEBUG -- : [0;30;101m[httplog] Connecting: login.yandex.ru:443[0m
D, [2017-04-26T19:17:42.609376 #24865] DEBUG -- : [0;30;101m[httplog] Sending: GET http://login.yandex.ru:443/info?format=json[0m
D, [2017-04-26T19:17:42.609720 #24865] DEBUG -- : [0;30;101m[httplog] Data: [0m
D, [2017-04-26T19:17:42.675702 #24865] DEBUG -- : [0;30;101m[httplog] Status: 200[0m
D, [2017-04-26T19:17:42.675972 #24865] DEBUG -- : [0;30;101m[httplog] Benchmark: 0.065791 seconds[0m
D, [2017-04-26T19:17:42.676211 #24865] DEBUG -- : [0;30;101m[httplog] Response:
{"first_name": "xxxxxxxxxxx9", "last_name": "xxxxxxxxxxxxxxx", "display_name": "xxxxx", "emails": ["xxxxxx@yandex.ru"], "default_email": "xxxxx@yandex.ru", "real_name": "xxxxxx2", "login": "xxxxxxx", "sex": "male", "id": "xxxx123"}[0m

问题:我如何将 Omniauth 身份验证过程中的 access_token、刷新令牌保存给用户,因为它很明显(第 7 行)它确实使用我的 client_id 和 secret 检索它,而无需我的任何代码.

谢谢。

我尝试过的:

在用户模型中将user.access_token = auth.credentials.token 添加到self.from_omniauth(auth) 方法。但没有任何积极的变化。

更新

同样的问题存在于不同的平台上。例如,在共享主机和 Linux Ubuntu 中(从头开始的全新项目)。

【问题讨论】:

  • 你试过调试CallbackControlleryandex动作中的参数吗?像puts params 之类的东西?我很确定 params 中返回的 access_token 不在 auth 哈希中。
  • @Зелёный ,是的,我试过了。我只得到“状态”和“代码”参数。
  • 请通过p response.headers.fetch('Access-Token')p response.headers.fetch('access_token')p response.headers 分享您获得的信息

标签: ruby-on-rails ruby ruby-on-rails-3 devise omniauth


【解决方案1】:

访问令牌由 yandex gem 提供,或者更具体地说,omniauth-oauth2 gem。

只需执行以下操作即可进入您的控制器:request.env["omniauth.auth"].credentials.token

轰隆隆。那是您的访问令牌。 (刷新令牌和过期数据也在credentials哈希中,打印出来就行了)

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 2011-07-12
    • 2012-11-04
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多