大块上传媒体到twitter uisng ruby​​ 1.9.3

时间:2018-02-27 11:36:34

标签: ruby api ruby-on-rails-3 twitter

我正在将大型视频上传到Twitter并获取错误

media = File.open('/home/geobeats/Downloads/test.mp4', 'rb')
segment_id = 0
bytes_sent = 0
access_token = Oauth::Twitter.new.access_token
resp_1 = access_token.post("https://upload.twitter.com/1.1/media/upload.json", 
  {command: 'INIT', media_type: 'video/mp4', total_bytes: media.size, media_category: 'tweet_video'})

while bytes_sent < media.size
  chunk = media.read(4*1024*1024).encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  req = access_token.post("https://upload.twitter.com/1.1/media/upload.json", 
    {command: 'APPEND', media_id: JSON.parse(resp_1.body)['media_id'], segment_index: segment_id, media: chunk})

  p req.body

  segment_id = segment_id + 1
  bytes_sent = media.tell
end

每次在APPEND电话上发出错误

0 个答案:

没有答案