如何使用 oauth gem 和 twitter 1.0.0 gem 对 Rails 应用程序中的用户进行身份验证?

发布于 2024-10-04 14:34:42 字数 1890 浏览 2 评论 0原文

Jnunemaker 刚刚更新了他的 twitter gem (https://github.com/jnunemaker/twitter) 并删除了 Twitter::Oauth 类。我的代码看起来不太像他的示例,所以我在更新它时遇到问题。这是我的代码在 twitter 0.9 gem 中的外观:

UsersController

def oauth
  consumer = Twitter::OAuth.new('mykey','mysecret')
  request_token = consumer.request_token  
  session[:request_token] = request_token.token  
  session[:request_token_secret] = request_token.secret
  redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end

def callback
  consumer = Twitter::OAuth.new('mykey','mysecret')
  atoken, asecret = oauth.authorize_from_request(session[:request_token], session[:request_token_secret], params[:oauth_verifier])  
  consumer.authorize_from_access(atoken,asecret)
  user = Twitter::Base.new(consumer).verify_credentials

  #and then I create a new user in my application, with attributes such as the user's follower count, etc
end

这是我尝试更改此代码的示例:

UsersController

def oauth
  consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
  request_token = consumer.get_request_token
  session[:request_token] = request_token.token  
  session[:request_token_secret] = request_token.secret
  redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end

def callback
  consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
  request_token = session[:request_token]
  atoken = OAuth::RequestToken.new(consumer, request_token.token, request_token.secret).get_access_token(:oauth_verifier => params[:oauth_verifier])
  consumer.authorize_from_access(atoken)
  user = Twitter::Client.new(consumer).verify_credentials

Gemfile

...
gem 'oauth'

我确信我的回调方法中有很多问题,但其中一个奇怪的是,当我在本地运行时,我的 oauth 方法工作正常,但当我尝试从我的实时版本(使用 heroku 部署)时,会出现“502 Bad Gateway”错误。

Jnunemaker just updated his twitter gem (https://github.com/jnunemaker/twitter) and removed the Twitter::Oauth class. My code doesn't look much like his example, so I'm having issues updating it. Here's what my code used to look with the twitter 0.9 gem:

UsersController

def oauth
  consumer = Twitter::OAuth.new('mykey','mysecret')
  request_token = consumer.request_token  
  session[:request_token] = request_token.token  
  session[:request_token_secret] = request_token.secret
  redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end

def callback
  consumer = Twitter::OAuth.new('mykey','mysecret')
  atoken, asecret = oauth.authorize_from_request(session[:request_token], session[:request_token_secret], params[:oauth_verifier])  
  consumer.authorize_from_access(atoken,asecret)
  user = Twitter::Base.new(consumer).verify_credentials

  #and then I create a new user in my application, with attributes such as the user's follower count, etc
end

Here's an example of what I've tried to do to change this code:

UsersController

def oauth
  consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
  request_token = consumer.get_request_token
  session[:request_token] = request_token.token  
  session[:request_token_secret] = request_token.secret
  redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end

def callback
  consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
  request_token = session[:request_token]
  atoken = OAuth::RequestToken.new(consumer, request_token.token, request_token.secret).get_access_token(:oauth_verifier => params[:oauth_verifier])
  consumer.authorize_from_access(atoken)
  user = Twitter::Client.new(consumer).verify_credentials

Gemfile

...
gem 'oauth'

I'm sure there are a number of things wrong in my callback method, but one thing that's weird is that my oauth method works fine when I'm running locally, but gives me a '502 Bad Gateway' error when I try from my live (deployed with heroku) version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吲‖鸣 2024-10-11 14:34:42

如果您无法让它与您现在拥有的功能一起使用,我已经能够将 Omniauth gem 与 Twitter gem 一起使用。 Omniauth 的设置非常简单。

要使用 Twitter gem,只需获取访问令牌信息即可Omniauth 回调完成后:

token = omniauth['credentials']['token'], 
secret = omniauth['credentials']['secret']

然后只需在使用 Twitter gem 方法之前设置 Twitter gem 配置设置

Twitter.oauth_token = token
Twitter.oauth_token_secret = secret

Twitter.home_timeline.first.text

(如果尚未在初始化程序文件中设置,则必须配置 Twitter gem Consumer_key 和 Consumer_key_secret ...)

If you can't get it to work with what you have now, I have been able to use the Omniauth gem together with the Twitter gem. Omniauth is very easy to setup.

To use the Twitter gem, just get the access token info after the Omniauth callback is done:

token = omniauth['credentials']['token'], 
secret = omniauth['credentials']['secret']

Then just set the Twitter gem config settings before using the Twitter gem methods

Twitter.oauth_token = token
Twitter.oauth_token_secret = secret

Twitter.home_timeline.first.text

(You'll have to configure the Twitter gem consumer_key and consumer_key_secret if you don't haven't that already set up in an initializer file...)

疧_╮線 2024-10-11 14:34:42

我很幸运地使用了

Authlogic + AuthLogic Connect。

我不确定您是否需要手动实现 oauth,但 gem 可能值得研究。

https://github.com/viatropos/authlogic-connect

我发现的唯一问题oauth 提供商有时如果回调 url 无法识别,它们会提供糟糕的错误消息,回调 url 是在您获取 api 密钥的位置配置的。

-肯

I've had good luck with

Authlogic + AuthLogic Connect.

I'm not sure if you need to implement the oauth by hand, but the gem might be worth looking into.

https://github.com/viatropos/authlogic-connect

The only gotcha I've found with oauth providers is sometimes they provide poor error messages if the callback url isn't recognized, which is configured where you get the api keys.

-Ken

冰火雁神 2024-10-11 14:34:42

你的例子很接近。控制器操作的正确代码如下所示:

  def new
    consumer = OAuth::Consumer.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, site: 'https://api.twitter.com', request_endpoint: 'https://api.twitter.com', authorize_path: '/oauth/authenticate')

    unless params[:oauth_token]
      request_token = consumer.get_request_token({ oauth_callback: request.original_url })
      session[:request_token] = { token: request_token.token, secret: request_token.secret}
      redirect_to request_token.authorize_url(force_login: 'true')
    else
      request_token = OAuth::RequestToken.from_hash(consumer, oauth_token: session[:request_token]["token"], oauth_token_secret: session[:request_token]["secret"])
      access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
      session[:request_token] = nil

      @client = Twitter::REST::Client.new do |config|
        config.consumer_key        = YOUR_CONSUMER_TOKEN
        config.consumer_secret     = YOUR_CONSUMER_SECRET
        config.access_token        = access_token.token
        config.access_token_secret = access_token.secret
      end
    end
  end

You were close in your example. The right code for your controller action would be something like this:

  def new
    consumer = OAuth::Consumer.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, site: 'https://api.twitter.com', request_endpoint: 'https://api.twitter.com', authorize_path: '/oauth/authenticate')

    unless params[:oauth_token]
      request_token = consumer.get_request_token({ oauth_callback: request.original_url })
      session[:request_token] = { token: request_token.token, secret: request_token.secret}
      redirect_to request_token.authorize_url(force_login: 'true')
    else
      request_token = OAuth::RequestToken.from_hash(consumer, oauth_token: session[:request_token]["token"], oauth_token_secret: session[:request_token]["secret"])
      access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
      session[:request_token] = nil

      @client = Twitter::REST::Client.new do |config|
        config.consumer_key        = YOUR_CONSUMER_TOKEN
        config.consumer_secret     = YOUR_CONSUMER_SECRET
        config.access_token        = access_token.token
        config.access_token_secret = access_token.secret
      end
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文