Upload Video - INIT - media parameter is missing



Hello everyone,

Currently I’m developing a twitter video uploader with ruby. I’ve successfully uploaded images, but never got a video to upload correctly. From my understanding, to start a video upload, you’d post to:

url_upload = "https://upload.twitter.com/1.1/media/upload.json"

and give the parameters: command, media_type, and total_bytes.

response = HTTP.auth(twitter_header_oauth_video).post(url_upload, :body => 'command=INIT&media_type=video/mp4&total_bytes=4430752')

but most times I get this response:
{"errors":[{"code":38,"message":"media parameter is missing."}]}

I believe I’m doing OAuth correctly, because I don’t get errors related to that.
Am I going about this the correct way?

Or has anyone used HTTParty or HTTP etc… to upload a video to twitter and wouldn’t mind sharing an example? I’ve seen the twurl examples, but I can’t use twurl for my application.



oauth_params = {
  :command => "INIT",
  :total_bytes => '4430752',
  :media_type => "video/mp4",
  :oauth_consumer_key => TWITTER_CONSUMER_ID,
  :oauth_nonce => nonce,
  :oauth_signature_method => "HMAC-SHA1",
  :oauth_timestamp => unix_epoch,
  :oauth_token => twitter_user.access_token,
  :oauth_version => "1.0"

oauth_signature_video = create_oauth_signature(twitter_user, http_method, url_upload, oauth_params)
twitter_header_oauth_video = create_oauth_header(twitter_user, nonce, unix_epoch, oauth_signature_video)

body_params_video_init = {
  :command => "INIT",
  :total_bytes => '4430752',
  :media_type => "video/mp4"

response = HTTParty.post(url_upload, :headers => {"Authorization" => twitter_header_oauth_video}, :body => body_params_video_init)

{"media_id"=>669242781429202944, "media_id_string"=>"669242781429202944", "expires_after_secs"=>86399}

The confusion is in which parameters are actually being calculated in the OAuth signature. I thought only the oauth_* params were used in calculating this, but no, apparently all parameters in this case.



twitter_header_oauth_video = 'OAuth oauth_consumer_key="keyrealhere", oauth_nonce="nbvpGdHaiVqLnhusJCyEjrKrwfsopiOOktbRjaEBPR", oauth_signature="notreal1xctoevNX9SSQz9Pk%3D%0A", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1448393684", oauth_token="realtokenhere", oauth_version="1.0"'


I know it’s been a long time, but I’m in a similar predicament (Upload chunked video with Oauth Echo), also using HTTParty. Can you explain what’s happening in create_oauth_signature and create_oauth_header? twitter_header_oauth_video looks to only contain oauth params, so I’m not sure where these guys :command => "INIT", :total_bytes => '4430752', :media_type => "video/mp4", come into play from oauth_params. And thanks for posting this!


Hey @baileyspace

create_oauth_signature creates a legit OAuth 2.0 signature from all the params in the POST requests. In this instance that includes all the body params and the oauth params that would be passed along in the header.

create_oauth_header actually creates the header ( that will be passed in the POST ) with that newly created signature, and needs to be formatted very nicely.

The docs are here explaining both processes:

twitter_header_oauth_video only contains the oauth params, because only the oauth params are passed in the header. The rest of the params are passed in the body of the HTTP POST

An example of a header pulled straight from the twitter documentation:

        OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog", 

As you can see, they only list the oauth params here.

Uploading a video to twitter is a pain generally speaking, and this is the first step in multiple HTTP calls. I’ve recently uploaded a github project that has the full picture. It’s a proof of concept. You’ll have to hack to get it to work, but the logic is correct, and shows how to create nonce’s, oauth headers, oauth signatures, and anything else related to video uploads. I’ve tweeted videos with it ( after some hacking ). It also explains a bit more on the different HTTP calls. Happy hacking :wink: