2013-08-05 61 views
0
consumer_key=my_consumer_key 
consumer_secret=my_consumer_secret 
access_token=my_access_token 
access_secret=my_access_secret 

consumer = OAuth::Consumer.new(consumer_key,consumer_secret,{:site => "https://api.twitter.com",:scheme => :header,:authorize_path => '/oauth/authenticate'}) 
token_hash = { :oauth_token =>access_token,:oauth_token_secret =>access_secret} 

#access_token = OAuth::AccessToken.new(
# consumer, 
    #config['oauth_token'], 
    #config['oauth_token_secret']) 
access_token = OAuth::AccessToken.from_hash(consumer, token_hash) 
img = open("https://s3.amazonaws.com/fbappshq-assets/datas/7828/thumb.png") 
#image_path = File.expand_path('https://s3.amazonaws.com/fbappshq-assets/datas/7828/thumb.png', File.dirname(__FILE__)) 
boundary = 'cce6735153bf14e47e999e68bb183e70a1fa7fc89722fc1efdf03a917340' 
request_body = '' 
request_body.concat("--#{boundary}\r\n") 
request_body.concat("Content-Disposition: form-data; name=\"status\";\r\n") 
request_body.concat("\r\n") 
request_body.concat("#{Time.now.to_s}\r\n") 
request_body.concat("--#{boundary}\r\n") 
#request_body.concat("Content-Disposition: form-data; name=\"media[]\"; filename=\"#{File.basename(image_path)}\"\r\n") 
request_body.concat("Content-Disposition: form-data; name=\"media[]\"; filename=\"#{img}\"\r\n") 
#request_body.concat("Content-Type: application/x-www-form-urlencoded\r\n") 
request_body.concat("Content-Type:multipart/form-data\r\n") 
request_body.concat("Accept-Encoding:gzip\r\n") 
request_body.concat("Content-Transfer-Encoding: binary\r\n") 
request_body.concat("\r\n") 
image_path='https://s3.amazonaws.com/fbappshq-assets/datas/7828/thumb.png' 
open(image_path, 'rb') do |f| 
    request_body.concat(f.read) 
end 
request_body.concat("\r\n") 
request_body.concat("--#{boundary}--\r\n") 

#uri = URI.parse('https://upload.twitter.com/1.1/statuses/update_with_media.json') 
uri=URI.parse("https://api.twitter.com/1.1/statuses/update_with_media.json") 
https = Net::HTTP.new(uri.host, uri.port) 
https.use_ssl = true 
https.start do |session| 
    request = Net::HTTP::Post.new(uri.request_uri) 
    request.set_content_type("multipart/form-data; boundary=#{boundary}") 
    request.set_content_type("multipart/form-data") 
    request.body = request_body 
    request['Content-Length'] = request.body.bytesize 
    a=access_token.sign!(request) 
    b= session.request(request) 
    puts "b---#{b.body}" 
end 

它給我們圖像不被張貼在Twiitter

{"errors":[{"message":"Internal error","code":131}]} 

我怎樣才能解決這個請幫助我。

+0

使用http://rubygems.org/gems/socialshare gem在twitter上輕鬆更新.. –

回答

2

socialshare寶石,你可以在Facebook上分享,微博,LinkedIn容易..所以你的要求,你可以做到這一點以這種方式發佈的圖像,

s = Socialshare::Tweeter.new(:consumer_key => TWITTER_CONSUMER_KEY, 
        :consumer_secret => TWITTER_CONSUMER_SECRET, 
        :token => twitter_user_token, 
        :secret => twitter_user_secret) 

s.twitter_user.update_with_media("text", your_file_path) 

所以這將消息(文字,圖片)上傳圖片到twitter,你可以在這個gem here上獲得更多信息。

+0

它給我們當我在Twitter上發佈圖片時,無法驗證您的錯誤。它只適用於twitter上簡單的文字發佈 –