如何使用ruby omniauth-twitter gem获取oauth令牌和oauth令牌秘密?

时间:2013-03-26 20:48:59

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

我设置我的应用程序类似于此处的教程 - http://railscasts.com/episodes/235-devise-and-omniauth-revised。如果你无法访问它,下面是我的代码

Omniauth控制器回调

  def all
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :twitter, :all
end

用户模型

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
      user.name = auth.info.name

    end
  end

  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

现在我想知道如何获取经过身份验证的用户的oauth令牌和oauth令牌密钥?

由于

1 个答案:

答案 0 :(得分:3)

如果您想查看某个提供商返回的信息,请将其作为回调控制器的第一行:

raise env["omniauth.auth"].to_yaml

您将能够看到可以通过auth.credentials.tokenauth.credentials.secret访问所需信息。

编辑:现在Rails 4使用了better_errors gem,这种检查omniauth哈希的方法不再有效。现在更好的方法是:

render :text => "<pre>" + env["omniauth.auth"].to_yaml and return