导轨的Twitter gem总是在第一次状态更新时失败(包括控制台示例)

时间:2023-01-16 12:00:58

Every time I try to post to twitter in my rails app, the first post fails. I will explain the issue further after I show a quick example. Here is a live example from the console (with sensitive stuff edited):

每当我尝试在我的rails应用程序中发布到Twitter时,第一篇文章都会失败。在我展示一个简单的例子后,我将进一步解释这个问题。这是一个来自控制台的实例(编辑了敏感内容):

>> u = User.find(7)
=> #<User id: 7, login: "corgan1003@gmail.com", email:
"corgan1003@gmail.com", crypted_password: "stuff", salt: "stuff",
twitter_token: "some_twitter_token", twitter_secret:
"some_twitter_secret", twitter_sn: "some_twitter_sn">

>> u.twitter_client
=> #<Twitter::Base:0x7f11672957a0 @client=#<Twitter::OAuth:
0x7f1167295728 @csecret="some_c_secret", @ctoken="some_c_token",
@asecret="some_a_secret", @consumer_options={},
@atoken="some_a_token">>

>> u.twitter_client.update("Hello World")
=> false

>> u.twitter_client
=> #<Twitter::Base:0x7f11672957a0 @client=#<Twitter::OAuth:
0x7f1167295728 @csecret="some_c_secret", @consumer=#<OAuth::Consumer:
0x7f1167290bd8 @uri=#<URI::HTTP:0x3f88b3948330 URL:http://
twitter.com>, @options={:access_token_path=>"/oauth/
access_token", :site=>"http://
twitter.com", :oauth_version=>"1.0", :scheme=>:header, :request_token_path=>"/
oauth/request_token", :signature_method=>"HMAC-
SHA1", :http_method=>:post, :authorize_path=>"/oauth/authorize"},
@http=#<Net::HTTP twitter.com:80 open=false>, @secret="secret",
@key="key">, @access_token=#<OAuth::AccessToken:0x7f1167290c78
@consumer=#<OAuth::Consumer:0x7f1167290bd8 @uri=#<URI::HTTP:
0x3f88b3948330 URL:http://twitter.com>, @options=
{:access_token_path=>"/oauth/access_token", :site=>"http://
twitter.com", :oauth_version=>"1.0", :scheme=>:header, :request_token_path=>"/
oauth/request_token", :signature_method=>"HMAC-
SHA1", :http_method=>:post, :authorize_path=>"/oauth/authorize"},
@http=#<Net::HTTP twitter.com:80 open=false>, @secret="secret",
@key="key">, @token="token", @response=#<Net::HTTPRequestTimeOut 408
Request Timeout readbody=true>, @secret="secret">, @ctoken="ctoken",
@asecret="asecret", @consumer_options={}, @atoken="atoken">>

>> u.twitter_client.update("Hello World")
=> <Mash created_at="Thu Sep 03 01:38:21 +0000 2009" favorited=false
id=3724099662 in_reply_to_screen_name=nil in_reply_to_status_id=nil
in_reply_to_user_id=nil source="<a href=\"http://www.url.com\" rel=
\"nofollow\">MyApp</a>" text="Hello World" truncated=false user=<Mash
created_at="Tue Aug 11 19:47:26 +0000 2009" description=nil
favourites_count=0 followers_count=4 following=false friends_count=21
id=64803633 location=nil name="some name" notifications=false
profile_background_color="9ae4e8" profile_background_image_url="http://
s.twimg.com/a/1251923748/images/themes/theme1/bg.gif"
profile_background_tile=false profile_image_url="http://s.twimg.com/a/
1251923748/images/default_profile_normal.png"
profile_link_color="0000ff" profile_sidebar_border_color="87bc44"
profile_sidebar_fill_color="e0ff92" profile_text_color="000000"
protected=false screen_name="screen_name" statuses_count=4
time_zone=nil url=nil utc_offset=nil verified=false>>

>> u.twitter_client
=> #<Twitter::Base:0x7f11672957a0 @client=#<Twitter::OAuth:
0x7f1167295728 @csecret="csecret", @consumer=#<OAuth::Consumer:
0x7f1167290bd8 @uri=#<URI::HTTP:0x3f88b3948330 URL:http://
twitter.com>, @options={:access_token_path=>"/oauth/
access_token", :site=>"http://
twitter.com", :oauth_version=>"1.0", :scheme=>:header, :request_token_path=>"/
oauth/request_token", :signature_method=>"HMAC-
SHA1", :http_method=>:post, :authorize_path=>"/oauth/authorize"},
@http=#<Net::HTTP twitter.com:80 open=false>, @secret="secret",
@key="key">, @access_token=#<OAuth::AccessToken:0x7f1167290c78
@consumer=#<OAuth::Consumer:0x7f1167290bd8 @uri=#<URI::HTTP:
0x3f88b3948330 URL:http://twitter.com>, @options=
{:access_token_path=>"/oauth/access_token", :site=>"http://
twitter.com", :oauth_version=>"1.0", :scheme=>:header, :request_token_path=>"/
oauth/request_token", :signature_method=>"HMAC-
SHA1", :http_method=>:post, :authorize_path=>"/oauth/authorize"},
@http=#<Net::HTTP twitter.com:80 open=false>, @secret="secret",
@key="key">, @token="token", @response=#<Net::HTTPOK 200 OK
readbody=true>, @secret="secret">, @ctoken="ctoken",
@asecret="asecret", @consumer_options={}, @atoken="atoken">>

It's obvious that doing u.twitter_client the first time is not setting the client properly as @consumer is not set and the first time I do u.twitter_client.update("Hello World"), if i look at the client after that I see the 408 request time out issue which is really strange to me. Does anyone know how I can fix this? I am including my functions below that the above example used.

很明显,第一次做u.twitter_client并没有正确设置客户端,因为没有设置@consumer并且我第一次执行u.twitter_client.update(“Hello World”),如果我在看到客户端后看到了408请求超时问题对我来说真的很奇怪。有谁知道我怎么解决这个问题?我在下面的例子中包含了我的功能。

def twitter_oauth
   @oauth ||= Twitter::OAuth.new(ConsumerConfig['twitter_token'],
ConsumerConfig['twitter_secret'])
end

def twitter_client
   @client ||= begin
     twitter_oauth.authorize_from_access(twitter_token,
twitter_secret)
     Twitter::Base.new(twitter_oauth)
   end
end

Update I should also note that this issue only occurs in my production environment. This process would tweet successfully in my development environment.

更新我还应该注意,此问题仅发生在我的生产环境中。这个过程会在我的开发环境中成功发推。

Thanks!

1 个解决方案

#1


0  

Do you need to change the credentials for your production environment? When you setup your application on the Twitter site now you specify what URL you want it to return to and it skips whatever URL you pass in programatically as a return (security feature).

您是否需要更改生产环境的凭据?现在,当您在Twitter站点上设置应用程序时,您可以指定要返回的URL,并以编程方式跳过您传递的任何URL作为返回(安全功能)。

#1


0  

Do you need to change the credentials for your production environment? When you setup your application on the Twitter site now you specify what URL you want it to return to and it skips whatever URL you pass in programatically as a return (security feature).

您是否需要更改生产环境的凭据?现在,当您在Twitter站点上设置应用程序时,您可以指定要返回的URL,并以编程方式跳过您传递的任何URL作为返回(安全功能)。