![](/img/trans.png)
[英]How do I NOT require user's email when using Rails Omniauth gem and Google OpenID
[英]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?
目前,我使用omniauth对用户进行身份验证。 在我的会话控制器中看起来像这样,并且运行良好:
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
我显然需要重新启动会话并将令牌和秘密放在正确的位置。 如何创建一个方法来执行此操作?
您需要在身份验证表( 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令牌和密钥存储为token
和token_secret
,并将提供者存储为twitter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.