twitter/twurl

Twurl simple Media upload fails with "media type unrecognized" (using ver 0.9.3)

willymena opened this issue · 2 comments

No matter what I do, I cannot get media upload to work. I keep getting "{"request":"/1.1/media/upload.json","error":"media type unrecognized."}"

I've tried this:
twurl -H upload.twitter.com -X POST /1.1/media/upload.json --file c:\media\tasfb-xml\twitter\tweet.png --file-field media

and this:
twurl -H upload.twitter.com "/1.1/media/upload.json" -f c:\media\tasfb-xml\twitter\tweet.png -F media -X POST

The only way I've been able to get around this, it by changing the twurl code as documented by nvanesch here:
#68

FWIW, I'm running under Windows 10-64bit. Error occurs under both PowerShell and regular cmd.

Here is the output using:
twurl -H upload.twitter.com -X POST /1.1/media/upload.json --file c:\media\tasfb-xml\twitter\tweet.png --file-field media -t

twurl : opening connection to upload.twitter.com:443...
At line:1 char:1
+ twurl -H upload.twitter.com -X POST /1.1/media/upload.json --file c:\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (opening connect...tter.com:443...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
opened
starting SSL for upload.twitter.com:443...
SSL established
<- 
"POST /1.1/media/upload.json HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: OAuth gem v0.5.4\r\nContent-Type: multipart/form-data, boundary=\"00Twurl647276245499362711lruwT99\"\r\nAuthorization: OAuth oauth_body_hash=\"RnKZKB%2FLr7J%2FSx9XfQLlBheu2ak%3D\", 
oauth_consumer_key=\"X1HBC8e9KVZEFpLW25FQUwO6P\", oauth_nonce=\"gEEXEl1ZKZatvutmViknXKhZyz2D6QDyxdj5iLcOLME\", oauth_signature=\"vfSzc182WW9pk2FxcZxuez8ttNA%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1527954061\", oauth_token=\"998636509895983104-mENZzzGvikG85p3HRFTIOVVFwhuq8Bc\", 
oauth_version=\"1.0\"\r\nConnection: close\r\nHost: upload.twitter.com\r\nContent-Length: 191\r\n\r\n"
<- "--00Twurl647276245499362711lruwT99\r\nContent-Disposition: form-data; name=\"media\"; filename=\"tweet.png\"\r\nContent-Type: application/octet-stream\r\n\r\n\x89PNG\n\r\n--00Twurl647276245499362711lruwT99--\r\n"
-> "HTTP/1.1 400 Bad Request\r\n"
-> "cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0\r\n"
-> "connection: close\r\n"
-> "content-disposition: attachment; filename=json.json\r\n"
-> "content-encoding: gzip\r\n"
-> "content-length: 96\r\n"
-> "content-type: application/json;charset=utf-8\r\n"
-> "date: Sat, 02 Jun 2018 15:41:01 GMT\r\n"
-> "expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
-> "last-modified: Sat, 02 Jun 2018 15:41:01 GMT\r\n"
-> "pragma: no-cache\r\n"
-> "server: tsa_a\r\n"
-> "set-cookie: personalization_id=\"v1_uBRn96ucBnT7RUoqjFxGew==\"; Expires=Mon, 01 Jun 2020 15:41:01 GMT; Path=/; Domain=.twitter.com\r\n"
-> "set-cookie: lang=en; Path=/\r\n"
-> "set-cookie: guest_id=v1%3A152795406185092731; Expires=Mon, 01 Jun 2020 15:41:01 GMT; Path=/; Domain=.twitter.com\r\n"
-> "status: 400 Bad Request\r\n"
-> "strict-transport-security: max-age=631138519\r\n"
-> "vary: Origin\r\n"
-> "x-access-level: read-write\r\n"
-> "x-connection-hash: c501a4786a5ad633207d5a4aedf32a51\r\n"
-> "x-frame-options: SAMEORIGIN\r\n"
-> "x-rate-limit-limit: 415\r\n"
-> "x-rate-limit-remaining: 395\r\n"
-> "x-rate-limit-reset: 1527954271\r\n"
-> "x-response-time: 18\r\n"
-> "x-transaction: 0009793300ea08ae\r\n"
-> "x-tsa-request-body-time: 1\r\n"
-> "x-twitter-response-tags: BouncerCompliant\r\n"
-> "x-xss-protection: 1; mode=block\r\n"
-> "\r\n"
reading 96 bytes...
-> ""
-> "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x00\xAAV*J-,M-.Q\xB2R\x8A\xD17\xD43\x8C\xD1\xCFMM\xC9L\x8C\xD1/-\xC8\xC9OL\xD1\xCB*\xCE\xCFS\xD2QJ-*\xCA/\x02\xAA\x01K*\x94T\x16\xA4*\x94\xE6\x15\xA5&\xE7\xA7\xE7eV\xA5\xA6\xE8)\xD5\x02\x00\x00\x00\xFF\xFF\x03\x00\xAC\x7F\t\xF4J\x00\x00\x00"
read 96 bytes
Conn close

Ruby introduced IO.binread since 1.9.2:
https://ruby-doc.org/core-1.9.3/IO.html#method-c-binread

so that's more easy to fix if this is acceptable.

https://github.com/twitter/twurl/blob/master/lib/twurl/oauth_client.rb#L99-L104

          if options.upload['base64']
-            enc = Base64.encode64(File.read(filename))
+            enc = Base64.encode64(File.binread(filename))
            multipart_body << enc
          else
-            multipart_body << File.read(filename)
+            multipart_body << File.binread(filename)
          end

The fix is merged into master, closing.