2010-12-16 83 views
0

目前我使用omniauth验证用户。这看起来像这样在我的会话控制器,并且效果很好:当用户已经登录时,如何为使用Twitter gem和Omniauth gem的用户获取访问令牌?

def create 
    auth = request.env['omniauth.auth'] 
    unless @auth = Authentication.find_from_hash(auth) 
    # Create a new user or add an auth to existing user, depending on 
    # whether there is already a user signed in. 
    @auth = Authentication.create_from_hash(auth, current_user) 
    end 
    # Log the authorizing user in. 
    self.current_user = @auth.user 

    redirect_to authentications_url, :notice => "You've signed in!" 
end 

在此之后,我一直保存在我的认证表Twitter的UID(我还使用LinkedIn,Facebook的),我认为 Twitter的会议已经被关了。

我现在如何进行身份验证,以便我可以使用Twitter宝石?如果我在omniauth回调之后立即调用它,我认为它应该是这样的。

token = auth['credentials']['token'], 
    secret = auth['credentials']['secret'] 
    Twitter.oauth_token = token 
    Twitter.oauth_token_secret = secret 

我显然需要重新启动会话,并把令牌和秘密在正确的地方。我如何创建一个方法来做到这一点?

回答

2

您需要在认证表(Authentication.create_from_hash)中存储令牌和Twitter提供的密码。只要你这样做,这应该工作:

twitter_credentials = current_user.authorizations.find_by_provider(:twitter) 

Twitter.oauth_token = twitter_credentials.token 
Twitter.oauth_token_secret = twitter_credentials.token_secret 

这是假设,在你认证表您存储Twitter的令牌和秘密为tokentoken_secret,以及存储供应商如twitter

+0

未定义的方法'授权'为#<用户:0x007fd5e2081398> – Rob 2014-04-13 17:31:43

相关问题