2010-12-16 82 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

相關問題