Help with twurl and media/upload.json


#1

My goal is post tweets with inline images using the API.

I have downloaded twurl and installed it and was able to post a test tweet without an image successfully.

Now I am trying to add an image to my test tweet.

Here is my API call:

twurl -H upload.twitter.com -X POST “/1.1/media/upload.json” --file “X:\Users\thomas.white\Desktop\mm_image.png” --file-field “media”

And here is the response I get back:

{“request”:"/1.1/media/upload.json",“error”:“not recognized.”}

Can anyone shed some light on what I’m doing wrong ? Thanks in advance !


#2

Hey @CML_VisualFeed,

I think this is likely caused by the quotes around the file path. Removing those should do the trick. If that doesn’t, and the below doesn’t help, would be great to confirm you are using version 0.9.0 or newer, as we only added support for media uploads since that version.

Here’s a sample of using twurl to upload media:

twurl -H upload.twitter.com "/1.1/media/upload.json" -f ~/Desktop/B05iXOYCUAAGns4.jpg_large.jpg -F media -X POST

{
  "image": {
    "w": 1024,
    "h": 576,
    "image_type": "image/jpeg"
  },
  "media_id": 588185299844370432,
  "media_id_string": "588185299844370432",
  "size": 105489
}

Then to Tweet that media_id:

twurl "/1.1/statuses/update.json" -d "media_ids=588185299844370432&status=Sample Tweet with media_ids and twurl" 

{
  "created_at": "Wed Apr 15 03:51:40 +0000 2015",
  "id": 588187910811299842,
  "id_str": "588187910811299842",
  "text": "Sample Tweet with media_ids and twurl http://t.co/fyPdMIfcNM",
...
    "media": [
      {
        "id": 588185299844370432,
        "id_str": "588185299844370432",
        "indices": [
          38,
          60
        ],
        "media_url": "http://pbs.twimg.com/media/CCmnafNUkAA0R-8.jpg",
        "media_url_https": "https://pbs.twimg.com/media/CCmnafNUkAA0R-8.jpg",
        "url": "http://t.co/fyPdMIfcNM",
        "display_url": "pic.twitter.com/fyPdMIfcNM",
        "expanded_url": "http://twitter.com/jaakkosf/status/588187910811299842/photo/1",
        "type": "photo",
        "sizes": {
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "small": {
            "w": 340,
            "h": 191,
            "resize": "fit"
          },
          "medium": {
            "w": 600,
            "h": 337,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 576,
            "resize": "fit"
          }
        }
      }
    ]
  },
  "extended_entities": {
    "media": [
      {
        "id": 588185299844370432,
        "id_str": "588185299844370432",
        "indices": [
          38,
          60
        ],
        "media_url": "http://pbs.twimg.com/media/CCmnafNUkAA0R-8.jpg",
        "media_url_https": "https://pbs.twimg.com/media/CCmnafNUkAA0R-8.jpg",
        "url": "http://t.co/fyPdMIfcNM",
        "display_url": "pic.twitter.com/fyPdMIfcNM",
        "expanded_url": "http://twitter.com/jaakkosf/status/588187910811299842/photo/1",
        "type": "photo",
        "sizes": {
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "small": {
            "w": 340,
            "h": 191,
            "resize": "fit"
          },
          "medium": {
            "w": 600,
            "h": 337,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 576,
            "resize": "fit"
          }
        }
      }
    ]
  },
...
}

#3

I am also having trouble using the twurl version 0.9.2 to upload media using the v1.1/media/upload.json method. I can use the deprecated method (upload with media) fine, but I want the app I am working on to be compliant with the latest API version.

Here is my attempt at uploading a 41kb image. Have also tried with jpg (~160kb) , also to no avail.

C:\>twurl -H upload.twitter.com "/1.1/media/upload.json" -f /Temp/small1984a.png -F media -X POST -t


opening connection to upload.twitter.com...
opened
<- "POST /1.1/media/upload.json HTTP/1.1\r\nAccept: */*\r\nUser-Agent: OAuth gem v0.4.7\r\nContent-Type: multipart/form-data, boundary=\"00Twurl275460826989200448lruwT99\"\r\nAuthorization: OAuth oauth_body_hash=\"****\", oauth_consumer_key=\"****\", oauth_nonce=\"****\", oauth_signature=\"****\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"14293362
41\", oauth_token=\"****\", oauth_version=\"1.0\"\r\nConnection: close\r\nHost: upload.twitter.com\r\nContent-Length: 196\r\n\r\n"
<- "--00Twurl275460826989200448lruwT99\r\nContent-Disposition: form-data; name=\"media\"; filename=\"small1984a.png\"\r\nContent-Type: application/octet-stream\r\n\r\n\x89PNG\n\r\n--00Twurl275460826989200448lruwT99--\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-length: 65\r\n"
-> "content-type: application/json;charset=utf-8\r\n"
-> "date: Sat, 18 Apr 2015 05:50:45 GMT\r\n"
-> "expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
-> "last-modified: Sat, 18 Apr 2015 05:50:45 GMT\r\n"
-> "pragma: no-cache\r\n"
-> "server: tsa_a\r\n"
-> "set-cookie: lang=en\r\n"
-> "set-cookie: guest_id=v1%3A142933624569305147; Domain=.twitter.com; Path=/; Expires=Mon, 17-Apr-2017 05:50:45 UTC\r\n"
-> "status: 400 Bad Request\r\n"
-> "strict-transport-security: max-age=631138519\r\n"
-> "x-access-level: read-write\r\n"
-> "x-connection-hash: 71995f38ca44dbc2e980809655127dea\r\n"
-> "x-frame-options: SAMEORIGIN\r\n"
-> "x-response-time: 24\r\n"
-> "x-transaction: 4c2fa9cb2e211990\r\n"
-> "x-tsa-request-body-time: 309\r\n"
-> "x-twitter-response-tags: BouncerCompliant\r\n"
-> "x-xss-protection: 1; mode=block\r\n"
-> "\r\n"
reading 65 bytes...
-> ""
-> "{\"request\":\"\\/1.1\\/media\\/upload.json\",\"error\":\"not recognized.\"}"
{"request":"\/1.1\/media\/upload.json","error":"not recognized."}read 65 bytes
Conn close

In the case of jpg I get the following error instead:

-> "{\"request\":\"\\/1.1\\/media\\/upload.json\",\"error\":\"[JPEG] Ran out of bytes to parse (null).\"}"
{"request":"\/1.1\/media\/upload.json","error":"[JPEG] Ran out of bytes to parse (null)."}read 90 bytes

#4

This makes me a little bit suspicious that there could be a character escaping issue going on. I’ve personally tried this on Windows 8 in both Command Prompt and Powershell and don’t have a problem with that, so I’m curious.

What version of Ruby and Windows are you on?

This part is confusing me as well - looks like it isn’t reading the file correctly. Odd!


#5

Thanks for replying.

I have tried on both Windows XP (running in a VM) and Windows 7 (32-bit) using ruby version 1.9.3 in the case of XP, and will have to confirm the Windows 7 case when I get to work. (pretty sure it is a pre 2.0 version)

I will endeavour to try using twurl with Ruby 2.0+ to see if this might be the issue, which it very well could be come to think of it.
Either that or I wondered if I was actually authenticating correctly for this method, but as I said I have no problem using twurl to post a regular tweet etc.

I saw over on github there is someone with what looks to have the same issue:


#6

OK. So now using twurl on Ruby 2.2.2 on Win7 (32-bit). I get the following. Slightly different response but still doesn’t work.

C:\Temp>twurl -H upload.twitter.com "/1.1/media/upload.json" -f MissingPoints.png -F media -X POST -t
opening connection to upload.twitter.com:443...
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.4.7\r\nC
ontent-Type: multipart/form-data, boundary=\"00Twurl103947366701906953lruwT99\"\r\nAuthorization: OAuth oauth_body_hash=\"****\", oauth_consumer_key=\"****\", oauth_nonce=\"****\", oauth_signature=\"****\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1429579457\", oauth_token=\"****\", oauth_version=\"1.0\"\r\nConnection: close\r\nHost: upload.twitter.com\r\nContent-Length: 199\r\n\r\n"
<- "--00Twurl103947366701906953lruwT99\r\nContent-Disposition: form-data; name=\"media\"; filename=\"MissingPoints.png\"\r\nContent-Type: application/octet-stream\r\n\r\n\x89PNG\n\r\n--00Twurl103947366701906953lruwT99--\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: 90\r\n"
-> "content-type: application/json;charset=utf-8\r\n"
-> "date: Tue, 21 Apr 2015 01:24:20 GMT\r\n"
-> "expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
-> "last-modified: Tue, 21 Apr 2015 01:24:20 GMT\r\n"
-> "pragma: no-cache\r\n"
-> "server: tsa_a\r\n"
-> "set-cookie: lang=en\r\n"
-> "set-cookie: guest_id=v1%3A142957946027372317; Domain=.twitter.com; Path=/; Expires=Thu, 20-Apr-2017 01:24:20 UTC\r\n"
-> "status: 400 Bad Request\r\n"
-> "strict-transport-security: max-age=631138519\r\n"
-> "x-access-level: read-write\r\n"
-> "x-connection-hash: be60b53e07249b45084efcedb69566d9\r\n"
-> "x-frame-options: SAMEORIGIN\r\n"
-> "x-response-time: 18\r\n"
-> "x-transaction: 3ac763fc4b9a1f3c\r\n"
-> "x-tsa-request-body-time: 2\r\n"
-> "x-twitter-response-tags: BouncerCompliant\r\n"
-> "x-xss-protection: 1; mode=block\r\n"
-> "\r\n"
reading 90 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
\xC9\xCB/Q(JM\xCEO\xCF\xCB\xACJM\xD1S\xAA\x05\x00\x00\x00\xFF\xFF\x03\x00\xED\xAF\x8F\xF8A\x00\x00\x00"
{"request":"\/1.1\/media\/upload.json","error":"not recognized."}read 90 bytes
Conn close

#7

I’m having what appears to be the same issue. This is Windows 7 x64 Enterprise running Ruby 2.2.3p173 and Twurl 0.9.3

C:\Ruby22\DevKit>twurl -H upload.twitter.com "/1.1/media/upload.json" -f C:\Arcade\RocketLauncher\Media\Artwork\MAME\dkong\Screenshots\20150907111127.jpg -F media -X POST -t
opening connection to upload.twitter.com:443...
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.4.7\r\
nContent-Type: multipart/form-data, boundary=\"00Twurl902550531151302446lruwT99\"\r\nAuthorization: OAuth oauth_body_hash=\"jhGhG0kYsYVElRgi0anaTDEhjX
I%3D\", oauth_consumer_key=\"ZTg2mzETuHhAQ7Cj7gZDA8ywA\", oauth_nonce=\"y1c93OJ8MRSHd6NdwpK549mjSUifO908TtmS4xJY\", oauth_signature=\"sRKc6x5Omdpvz6C8
fMrqP63%2ByHE%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1441660228\", oauth_token=\"3090409445-l6HzXDdzG1JFTzi2G4RzCVVAhQALF1FGJb1
Z7Wc\", oauth_version=\"1.0\"\r\nConnection: close\r\nHost: upload.twitter.com\r\nContent-Length: 279\r\n\r\n"
<- "--00Twurl902550531151302446lruwT99\r\nContent-Disposition: form-data; name=\"media\"; filename=\"20150907111127.jpg\"\r\nContent-Type: application
/octet-stream\r\n\r\n\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xFF\xDB\x00C\x00\x04\x02\x03\x03\x03\x02\x04\x03\x03\x03\x04\x04\x
04\x04\x05\t\x06\x05\x05\x05\x05\v\b\b\x06\t\r\v\r\r\r\v\f\f\x0E\x10\x14\x11\x0E\x0F\x13\x0F\f\f\x12\x18\x12\x13\x15\x16\x17\x17\x17\x0E\x11\x19\e\x19
\x16\r\n--00Twurl902550531151302446lruwT99--\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: Mon, 07 Sep 2015 21:10:29 GMT\r\n"
-> "expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
-> "last-modified: Mon, 07 Sep 2015 21:10:29 GMT\r\n"
-> "pragma: no-cache\r\n"
-> "server: tsa_b\r\n"
-> "set-cookie: lang=en; Path=/\r\n"
-> "set-cookie: guest_id=v1%3A144166022920871137; Domain=.twitter.com; Path=/; Expires=Wed, 06-Sep-2017 21:10:29 UTC\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: 3b1c3d4b9c689f6526cc5d2e24aa4e75\r\n"
-> "x-frame-options: SAMEORIGIN\r\n"
-> "x-rate-limit-limit: 415\r\n"
-> "x-rate-limit-remaining: 413\r\n"
-> "x-rate-limit-reset: 1441663821\r\n"
-> "x-response-time: 17\r\n"
-> "x-transaction: 5c098d3cb328990f\r\n"
-> "x-tsa-request-body-time: 3\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\x
AA\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"
{"request":"\/1.1\/media\/upload.json","error":"media type unrecognized."}read 96 bytes
Conn close

#8

We’ve created a quick tool to help test your media file uploads, both using the ‘media’ (binary) and 'media_data" (base64) params.

http://django-rest-apis.herokuapp.com/media

Source available here:

Give it a shot and let us know how your file fares with this tool.


#9

Thank you rchoi, I tried the tool you have provided and it was able to post (@goochhazarcade) but when I try the same command from the CLI with twurl it does not work, same result as before.