0

目前我使用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 gem?我认为如果我在omniauth回调之后立即调用它应该是这样的。

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

我显然需要重新启动会话并将令牌和密码放在正确的位置。我怎样才能创建一个方法来做到这一点?

4

1 回答 1

2

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

twitter_credentials = current_user.authorizations.find_by_provider(:twitter)

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

假设在您的身份验证表中,您将 Twitter 令牌和秘密存储为tokenand token_secret,并将提供者存储为twitter

于 2011-02-19T10:15:05.943 回答