[英]How do I fetch the access token for a user using the Twitter gem and the Omniauth gem when the users is already logged in?
Currently I authenticate the user using omniauth. 目前,我使用omniauth对用户进行身份验证。 This looks like this in my sessions controller and works well: 在我的会话控制器中看起来像这样,并且运行良好:
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
After this, I've stored the twitter uid in my authentications table (I also use linkedin, facebook) and I think that the twitter sessions has been closed. 在此之后,我将twitter uid存储在我的身份验证表中(我也使用linkedin,facebook),我认为 Twitter会话已经关闭。
How do I now authenticate so that I can use the Twitter gem? 我现在如何进行身份验证,以便我可以使用Twitter宝石? I think it should be something like this if I was calling it right after the omniauth callback. 我认为如果在omniauth回调之后立即调用它,应该是这样的。
token = auth['credentials']['token'],
secret = auth['credentials']['secret']
Twitter.oauth_token = token
Twitter.oauth_token_secret = secret
I clearly need to restart the session and put the token and secret in the right place. 我显然需要重新启动会话并将令牌和秘密放在正确的位置。 How can I create a method to do this? 如何创建一个方法来执行此操作?
You need to store both the token and the secret provided by Twitter in your authentications table ( Authentication.create_from_hash
). 您需要在身份验证表( Authentication.create_from_hash
)中存储Twitter提供的令牌和秘密。 As long as you're doing that, this should work: 只要您这样做,它就应该起作用:
twitter_credentials = current_user.authorizations.find_by_provider(:twitter)
Twitter.oauth_token = twitter_credentials.token
Twitter.oauth_token_secret = twitter_credentials.token_secret
That's assuming that in your authentications table you store the Twitter token and secret as token
and token_secret
, as well as storing the provider as twitter
假设在身份验证表中,将Twitter令牌和密钥存储为token
和token_secret
,并将提供者存储为twitter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.