2013-03-26 39 views
1

我设置我的应用类似于这里的教程 - http://railscasts.com/episodes/235-devise-and-omniauth-revised。如果你不能访问它,下面是我的代码如何使用ruby omniauth-twitter gem获得oauth token和oauth token secret?

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令牌秘密?

感谢

回答

3

如果你想看看通过一定的供应商返回的信息,把这个作为你的回调控制器的第一行:

raise env["omniauth.auth"].to_yaml 

您可以看到您需要的信息可以在auth.credentials.tokenauth.credentials.secret中找到。

编辑:现在,Rails 4使用better_errors宝石,这种检查omniauth散列的方法不再适用。现在更好的方法是:

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

'raise request.env [“omniauth.auth”]。to_yaml'这非常方便。谢谢。 – psharma 2013-04-01 13:27:14

+1

我只使用'render json:request.env ['omniauth.auth']' – Nemo 2014-07-29 03:45:28

相关问题