When i post a tweet with img, I get The validation of media ids failed

api

#1

I get following response: {“errors”:[{“code”:324,“message”:“The validation of media ids failed.”}]} when i try to attach a img in tweet

here is my img
here is my code:

require 'signet/oauth_1/client'
require 'faraday'
require 'faraday_middleware'
require 'json'
require 'addressable/uri'

module Pj
  class << self
    def home(*args, &blk)
      File.join ENV['HOME'], *args, &blk
    end
  end
end


def auth_url(*args, &blk)
  "http://www.tumblr.com/oauth/authorize"
end

def token_url(*args, &blk)
  "http://www.tumblr.com/oauth/access_token"
end

def rq_token_url(*args, &blk)
  "http://www.tumblr.com/oauth/request_token"
end

def load_json(f, *args, &blk)
  JSON.load File.read(f)
end

def access_token(*args, &blk)
  @access_token ||= load_json(Pj.home('twitter/api.json'))['access_token']
end

def access_token_secret(*args, &blk)
  @access_token_secret ||= load_json(Pj.home('twitter/api.json'))['access_token_secret']
end

def key
  @key ||= load_json(Pj.home('twitter/api.json'))['consumer_key']
end

def secret
  @secret ||= load_json(Pj.home('twitter/api.json'))['consumer_secret']
end

def c
  @c ||= ::Signet::OAuth1::Client.new(
      temporary_credential_uri: rq_token_url,
      authorization_uri: auth_url,
      token_credential_uri: token_url,
      client_credential_key: key,
      client_credential_secret: secret,
      token_credential_key: access_token,
      token_credential_secret: access_token_secret
  )
end

def h(*args, &blk)
  @h ||= Faraday.new do |cnn|
    cnn.use ::FaradayMiddleware::FollowRedirects, limit: 10
    cnn.adapter :httpclient
  end
end

def post_with_token(url, *args, &blk)
  rsp = h.post(url) do |r|
    c.generate_authenticated_request(method: :post, uri: url).headers.each do |k, v|
      r.headers[k] = v
    end

    if block_given?
      yield r
    end
  end
  rsp
end

r = post_with_token("https://upload.twitter.com/1.1/media/upload.json") do |r|
  r.options.timeout = 120

  r.headers['content-type'] = "multipart/form-data"
  r.body = {
      media: File.binread(Pj.home("Dropbox/img/success.png"))
  }
end

puts r.body

r2 = post_with_token("https://api.twitter.com/1.1/statuses/update.json?status=#{CGI.escape(::Time.now.to_s)}&media_ids=#{JSON.load(r.body)['media_id_string']}")

puts r2.body


#2

And the output is

Fast Debugger (ruby-debug-ide 0.4.32, debase 0.1.7) listens on 127.0.0.1:33541
{"media_id":620444604152688641,"media_id_string":"620444604152688641","size":363,"expires_after_secs":3600,"image":{"image_type":"image\/png","w":1,"h":1}}
Cookie#domain returns dot-less domain name now. Use Cookie#dot_domain if you need "." at the beginning.
{"errors":[{"code":324,"message":"The validation of media ids failed."}]}