Hi,
I am trying to upload a simple image to Twitter using both Twitter gem and Twitter REST API. I always get the following error Twitter::Error::BadRequest: Segments do not add up to provided total file size.
If I am right, I understand that at the end of the process (FINALIZE), the size of my uploaded image (during the APPEND) is the not the same I declared in the first place (during the INIT).
Here is my code:
file_path = "/Users/folder/image.png"
filesize = File.open(file_path).size
init_request = Twitter::REST::Request.new(TWITTER, :post, "https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=#{filesize}&media_type=image/png").perform
media_id = init_request[:media_id]
base64_file = Base64.encode64(file_path).gsub("\n","")
Twitter::REST::Request.new(TWITTER, :post, "https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=#{media_id}&media=#{file_path}&media_data=#{base64_file}&segment_index=0").perform
Twitter::REST::Request.new(TWITTER, :post, "https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=#{media_id}").perform
Any hint? Thank you!