简体   繁体   English

当用户已经登录时,如何使用Twitter gem和Omniauth gem获取用户的访问令牌?

[英]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令牌和密钥存储为tokentoken_secret ,并将提供者存储为twitter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Rails Omniauth gem和Google OpenID时,如何不要求用户发送电子邮件 - How do I NOT require user's email when using Rails Omniauth gem and Google OpenID Twitter gem身份验证错误(使用omniauth-twitter令牌/秘密) - Twitter gem authentication error (using omniauth-twitter token/secret) 如何使用linkedin gem和omniauth获得更精细的LinkedIn用户信息? - How do I get more granular LinkedIn user information using linkedin gem and omniauth? 如何使用omniauth gem获取Twitter用户ID? - How to use omniauth gem to get twitter user id? 要在Twitter上发帖,我是否也需要omniauth gem(以及twitter gem)? - To post at twitter, do I need omniauth gem too (along with twitter gem)? 对于 rails 上的 twitter gem,我如何有效地调用 user_timeline 并为多个用户抓取推文? - For the twitter gem on rails, how do I efficiently call user_timeline and grab tweets for multiple users? 将omniauth-twitter gem与devise和rails一起使用5 - Using the omniauth-twitter gem with devise and rails 5 如何使用 oauth gem 和 twitter 1.0.0 gem 对 Rails 应用程序中的用户进行身份验证? - How do I authenticate users in a Rails app with the oauth gem and twitter 1.0.0 gem? 使用databasedotcom ruby​​ gem访问Salesforce时,如何获取特定用户(或帐户)的ActivityHistory - When using databasedotcom ruby gem to access salesforce, how do I get the ActivityHistory for a particular User (or Account) 如何使用omniauth-facebook gem获取Facebook个人资料数据? - How to fetch facebook profile data using omniauth-facebook gem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM