【问题标题】:How can I get oauth token and oauth token secret using the ruby omniauth-twitter gem?如何使用 ruby​​ omniauth-twitter gem 获取 oauth 令牌和 oauth 令牌秘密?
【发布时间】:2013-03-16 20:10:18
【问题描述】:

我设置我的应用程序类似于这里的教程 - 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 token 和 oauth token secret?

谢谢

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

    • raise request.env["omniauth.auth"].to_yaml 这太方便了。谢谢。
    • 我只用render json: request.env['omniauth.auth']
    猜你喜欢
    • 2012-04-28
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 2013-06-13
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多