Ruby twitter示例 - 客户必须提供“状态” - 内容类型问题

时间:2009-12-15 21:38:44

标签: ruby twitter

使用Ruby irb测试简单的Twitter状态更新时出现以下错误...

#<Net::HTTPForbidden:0x2ca15dc>
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Client must provide a 'status' parameter with a value.</error>
</hash>

使用的代码是:

twitHeaders = {
  "Content-Type"=>"text/html",
  "Authorization" => "Basic " + Base64::encode64("account:password")
  # where account is twitter account and password is twitter account password
}

def postTwitter statusMsg, headers
  #url = URI.parse( "http://twitter.com/statuses/update.xml" )
  url = URI.parse( "http://twitter.com/statuses/update.json" )
  http = Net::HTTP.new(url.host, url.port)
  resp, data = http.post(url.path, statusMsg , headers)
  return resp, data
end

puts postTwitter( "status=hohoho", twitHeaders )

.json和.xml都能有效地产生相同的结果

1 个答案:

答案 0 :(得分:1)

Content-Type必须为application/x-www-form-urlencoded