简体   繁体   English

如何使用 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?

Jnunemaker just updated his twitter gem (https://github.com/jnunemaker/twitter) and removed the Twitter::Oauth class. Jnunemaker 刚刚更新了他的 twitter gem (https://github.com/jnunemaker/twitter) 并删除了 Twitter::Oauth 类。 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:这是我的代码在 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.我确定我的回调方法有很多问题,但奇怪的是,当我在本地运行时,我的 oauth 方法工作正常,但是当我尝试从我的实时(使用 heroku 部署)版本。

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 gem 与 Twitter gem 一起使用。 Omniauth is very easy to setup. Omniauth 非常容易设置。

To use the Twitter gem, just get the access token info after the Omniauth callback is done:要使用 Twitter gem,只需在 Omniauth 回调完成后获取访问令牌信息:

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

Then just set the Twitter gem config settings before using the Twitter gem methods然后在使用 Twitter gem 方法之前设置 Twitter gem 配置设置

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...) (如果你还没有在初始化文件中设置,你必须配置 Twitter gem consumer_key 和 consumer_key_secret ......)

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

I've had good luck with我很幸运

Authlogic + AuthLogic Connect. Authlogic + AuthLogic 连接。

I'm not sure if you need to implement the oauth by hand, but the gem might be worth looking into.我不确定您是否需要手动实现 oauth,但该 gem 可能值得研究。

https://github.com/viatropos/authlogic-connect 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.我发现 oauth 提供商的唯一问题是,如果无法识别回调 url,它们有时会提供糟糕的错误消息,这是在您获取 api 密钥的位置配置的。

-Ken -肯

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

相关问题 对于 rails 上的 twitter gem,我如何有效地调用 user_timeline 并为多个用户抓取推文? - For the twitter gem on rails, how do I efficiently call user_timeline and grab tweets for multiple users? 当用户已经登录时,如何使用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? 如何在rails app中设置Twitter gem? - How to set up the Twitter gem in rails app? 在Rails 5应用中实现Twitter Gem - Implementing twitter gem in rails 5 app Rails 5和Knock gem:无法验证用户 - Rails 5 and Knock gem: can't authenticate users 如何使用twitter-bootstrap-rails gem禁用部分Twitter Bootstrap响应式设计? - How do I disable a portion of Twitter Bootstrap responsive design using twitter-bootstrap-rails gem? 如何在Gem中获取Rails应用程序的根目录 - How do I get the root directory of a Rails app in a Gem 在Rails中,如何为Rails设置Twitter Typeahead.js gem? - In Rails, how do I set up the Twitter Typeahead.js gem for Rails? 如何在Rails应用程序中正确解压缩宝石? - How do I properly gem unpack a gem in a rails application? 如何调试rails 3 gem? - How do I debug a rails 3 gem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM