每次我尝试在我的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">>
很明显,第一次做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
更新 我还应该注意,这个问题只发生在我的生产环境中。这个过程会在我的开发环境中成功发布。
谢谢!
答案 0 :(得分:0)
您是否需要更改生产环境的凭据?现在,当您在Twitter站点上设置应用程序时,您可以指定要返回的URL,并以编程方式跳过您传递的任何URL作为返回(安全功能)。