如何使用 oauth gem 和 twitter 1.0.0 gem 对 Rails 应用程序中的用户进行身份验证?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您无法让它与您现在拥有的功能一起使用,我已经能够将 Omniauth gem 与 Twitter gem 一起使用。 Omniauth 的设置非常简单。
要使用 Twitter gem,只需获取访问令牌信息即可Omniauth 回调完成后:
然后只需在使用 Twitter gem 方法之前设置 Twitter gem 配置设置
(如果尚未在初始化程序文件中设置,则必须配置 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:
Then just set the Twitter gem config settings before using the Twitter gem methods
(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...)
我很幸运地使用了
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
你的例子很接近。控制器操作的正确代码如下所示:
You were close in your example. The right code for your controller action would be something like this: