POST media/upload.json always returns “media parameter is missing"


#1

I’m working on an image-posting script from within Google Apps Script, and I can’t seem to get posting to https://upload.twitter.com/1.1/media/upload.json to work correctly.

After trying to build my own multipart form by concatenating all the bytes together, I’ve reduced the overhead to just one blob (type image/jpeg) in the payload. The options object looks like this:

var options = {
“oAuthServiceName”:“twitter”,
“oAuthUseToken”:“always”,
method: “POST”,
contentType: “multipart/form-data”,
payload: { “media[]” : imageblob }
};

where imageblob is a Blob (apps script type for handling binary data) containg the bin data for a JPEG and the image/jpeg content type.

One sample of the request object that’s created:
[14-11-20 06:23:11:240 PST]
{“headers”:{
“X-Forwarded-For”:”174.140.111.67”
},
“oAuthServiceName”:“twitter”,
“useIntranet”:false,
“followRedirects”:true,
“payload”:”-------wlinujDVABg2j6YW82NGhFaaNR5eDBAHsXqTWd1VmQHMTXtX7H\r\nContent-Disposition: form-data; name=“media[]”; filename=“edt35.jpg”\r\nContent-type: image/jpeg\r\n\r\n
[binary data truncated]
\r\n-------wlinujDVABg2j6YW82NGhFaaNR5eDBAHsXqTWd1VmQHMTXtX7H–\r\n”,
“oAuthUseToken”:"always”,
“method”:"post”,
“contentType”:"multipart/form-data”,
“validateHttpsCertificates”:true,
“url”:"https://upload.twitter.com/1.1/media/upload.json”
}

And what I get back is always:
{ "errors”: [{“code”:38,“message”:“media parameter is missing.”}]}

So I’ve tried every naming permutation (media, media[], media_data, media_data[]) and none of these produce any different an answer. The format of the form data being sent seems correct, so I am rather at a loss for what I’m doing wrong. Anyone dealt with this before?


#2

Figured it out finally. Two things are important here:

  1. the parameter name has to be “media”, not “media[]”
  2. Do not set the contentType property. It prevents a Content-Type header with the correct boundary value from being automatically created.

A correct options object looks like this:
var options = {
“oAuthServiceName”:“twitter”,
“oAuthUseToken”:“always”,
method: “POST”,
payload: { “media” : imageblob }
};


Media upload (upload.json) endpoint
#3

Glad you got it figured out—thanks for posting the solution, too, so others can benefit. Much appreciated.


#4

Thank you. If anyone reading this would like to leverage my work, I’ve put together a simple Twitter library for Apps Script that includes searching, tweeting, and uploading media. Check it out at https://script.google.com/d/11dB74uW9VLpgvy1Ax3eBZ8J7as0ZrGtx4BPw7RKK-JQXyAJHBx98pY-7/edit?usp=sharing


#5

I am trying to upload binary image directly…I am getting following error:
{“errors”:[{“message”:“Could not authenticate you”,“code”:32}]}

Can you please suggest me.