Jnunemaker刚刚更新了他的twitter gem(https://github.com/jnunemaker/twitter)并删除了Twitter :: Oauth类。我的代码看起来不像他的例子,所以我有更新它的问题。以下是我的代码用于查看twitter 0.9 gem的内容:
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
以下是我尝试更改此代码的示例:
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
...
gem 'oauth'
我确信我的回调方法中存在许多错误,但有一件事很奇怪,当我在本地运行时,我的oauth方法工作正常,但当我运行时出现'502 Bad Gateway'错误尝试从我的直播(使用heroku部署)版本。
答案 0 :(得分:2)
如果你无法使用它现在拥有的东西,我已经能够将Omniauth宝石与Twitter宝石一起使用。 Omniauth is very easy to setup.
要使用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 ......)
答案 1 :(得分:0)
我好运
Authlogic + AuthLogic Connect。
我不确定你是否需要手工实现oauth,但宝石可能值得研究。
https://github.com/viatropos/authlogic-connect
我发现oauth提供程序的唯一问题是,如果无法识别回调网址,有时它们会提供较差的错误消息,这是在获取api密钥的位置配置的。
-Ken
答案 2 :(得分:0)
你的例子很接近。控制器操作的正确代码如下:
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