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):
>> u = User.find(7)
=> #<User id: 7, login: "[email protected]", email:
"[email protected]", 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.
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!