Post image problems


#1

I want to upload an image (less then 100 kb) using REST API:

Endpoiny: https://upload.twitter.com/1.1/media/upload.json

I’m ok until the login process and this is the data i have:

{oauth_token: "191362404-hysx**********", oauth_token_secret: “xHAUodmFECILJew*******”, user_id: “19******”, screen_name: “j*****”, x_auth_expires: “0”}**

this is the http call:

Request URL:https://upload.twitter.com/1.1/media/upload.json
Request Method:POST
Status Code:401 Authorization Required
Request Headers

POST https://upload.twitter.com/1.1/media/upload.json HTTP/1.1
Accept: application/json, text/plain, /
Origin: file://
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; LG-V500 Build/KOT49I.V50020f) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36
Authorization: OAuth oauth_consumer_key=“TGOFLgj6iTj9****”,oauth_nonce=“w2S5***”,oauth_signature_method=“HMAC-SHA1”,oauth_token=“191362404-hysx8dJKOckaQm7Djxp****”,oauth_timestamp=“1501067412”,oauth_version=“1.0”,oauth_signature="x5P8ch%2BkSNerP1kJg5*****"
Content-Type: application/x-www-form-urlencoded;charset=UTF-8;

Form Data
{“media_data”:“iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAG61AAButQGgEB8NAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAp1QTFRF////AQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQACAQAC8j0gFAAAAN50Uk5TAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkbHB0eHyAhIiMlJicpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQ0RFRkdISUxOT1BRVFVXWFlaW1xdXmFkZWZpamtsbW5vcHFyc3R1dnd4eXp7fX5/gIGCg4SHiImLjI2Oj5CRkpOUlZaXmJmbnZ6foKGio6Slpqepqqusra6vsLK0tba4ury9vsDBw8TFxsfIycrLzM3O0NHS09TV1tjZ2tvc3d7f4OHi4+Tl5ufo6err7O7v8PHy8/T19vf4+fr7/P3+WRC2NgAABLlJREFUGBntwfdDlHUABvCHjefABYaKKzVzoJQjbeJAwUWlqTmjLMkiB1kqKakkVoiGVkphWCYUiIpRKUPS8lAx0eC4g3v+lgRFD+69u/f7fcdP9/kAfn5+fn5+upuQknGkqNZ27cyxzNTZoTBXSPzuWrr6NyexG0wT+sZNurubNQBKhu6dBF0FJNdQWcOGMHQ18oDjDFyFQKNxZfSsZj46GXu4lVwCV0vToUniXXq1IxAdAqZ+6yRZFwZXZVwLDVKd9OFoONpELMi+xnbr4WoK6VwMWcEH6dvp/hi34ScHHygLhquDJO3xkLSHatRe5SP28XAV1cx77sRByusUtxmdbGS7609CwosOCvsxFJ0U8b5b0yFsSD2F/doTnVn5Q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3p5za2Hb0hyaB8445KO3vzYOgXVTKBcpozZ8bBJ3EfVpPQVc+GAw9hS0saKZajtNpU4Ogu7Bpbx65Sp8q9yZFwDiDF+wotlFZ49ncjUnDYLywCTMXr0rblVtQWn3bUV97oaggb//O9QnDA+Hn5+fn56ef/wFO3e89ppoUBAAAAABJRU5ErkJggg==”}

Response Headers
content-encoding:gzip
content-length:89
content-type:application/json; charset=utf-8
date:Wed, 26 Jul 2017 11:10:07 GMT
server:tsa_f
strict-transport-security:max-age=631138519
vary:Origin
x-connection-hash:c6dddf421ec8c463585de4be4cdc121a
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-response-time:112
x-tsa-request-body-time:0
x-xss-protection:1; mode=block


A possible cause could be that i have to include:

“data:image/png;base64,”

Thank you.


#2

I just successfully uploaded this using twurl as follows:

$ twurl -t -H upload.twitter.com -X POST "/1.1/media/upload.json" -d "media_data=iVBORw0K...=(rest of base64 data removed for length)="

You could try this using your own application consumer key in twurl. The -t option will trace out the request and response headers.

Note that, even though this is a small image, the media/upload documentation also states that the chunked upload process is preferred and is the method that will be enhanced in the future, so if you are building something now, it may be a good idea to look at that instead.


#3

I have done a couple of tests and i have noticed that pasting the command including the base64 string (the file is 85kb size), the same string is cut when exceed the limit of 8.111 chars.

Instead the first test that contains less than that limit for the media_data was ok.

For example:

twurl -t -H upload.twitter.com -X POST “/1.1/media/upload.json” -d “media_data=iVBORw0KGgoAAAANSUhEUgAABIgAAAJECAYAAAB91h82AAAgAElEQVR4Xuy9C5wV1Z3vW7W7G2gEbdQY3…”

is that a limit of twurl tool or a possible cause of why i can’t upload the image?
Thank you very much.


#4

It’s probably a limit on the shell you’re using, at a guess. I assumed the image in question was the one you used in your earlier post. You should probably consider using the chunked upload method as I described - there’s a Python sample here (can easily be modified for image data instead of video).


#5

Ok thank you i will try to “translate” the code.
I’m currntly working with javascript + angular app.