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

它给了我们

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

我该如何解决这个问题请帮助我。

4

1 回答 1

2

使用socialshare gem,您可以轻松地在 facebook、twitter、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 的更多信息。

于 2013-08-05T08:31:37.740 回答