Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API - upload an image

I try to upload an image via Twitter api , as described there I created a form with name="status" and name="media[]" -

<form id="image-form">
    <input type="text" name="status">
    <input name="media[]" type="file" />
    <input type="submit" value="POST IMAGE">
</form>

in addition I have a submit handler -

   $('#image-form').submit( function(e) {
                e.preventDefault();
                var formData = new FormData(this); // <-- 'this' is your form element


        $.ajax({
                    url:'https://api.twitter.com/1.1/statuses/update_with_media.json',
                    type: 'POST',
                    contentType: false,
                    pagerocessData: false,
                    processData: false,
                    data: formData,
                    success: function(data) {
                        alert('Image upload succeeded');
                    },
                    error: function (responseData, textStatus, errorThrown) {
                        alert('GET failed.');
                    }
                });
            });

Under Networks at Chrome I see this request with Content-Type:multipart/form-data

finally I get "response 200" but it doesn't upload an image to the Twitter account ,

What I did wrong here ?

like image 258
URL87 Avatar asked Aug 12 '15 23:08

URL87


1 Answers

I couldn't post this as a comment, so I'm going to risk my life writing it as an answer :P.

I tried Exploring the Twitter API (on Firefox works, in Chrome it fails when loading it).

Steps to set up the testing

  • Service : Select https://api.twitter.com/1.1, it will show up some options. In the endpoints list select the one you are using.
  • Authentication : OAuth (try with a fake account if you are afraid of using your owns, I've created a fake one to test this. I tried with No auth, Basic Auth and none of them worked)
  • Request URL : POST, the url is set automatically.
  • Query tab : status - The tweet message.
  • Body tab : scroll down and look for media, select an image.

Finally press send.

What I can notice is that if I don't specify an image nor text it will fail (obviously). If I set an image without a status it will upload the image, and if I specify both, well... it will upload the image with the text.

Note that the status is appended to the URL.

So a few questions :

  • Did you set up OAuth?
  • Are you setting at least one of the required fields (status or media)? Are you passing the status through the URL?
  • When you say it works, but it doesn't upload the image, do you see the status in your twitter account?

Here's the result of the fake account I made.

I hope at least it gives you a hint of what it can be.

like image 145
Eric Martinez Avatar answered Oct 08 '22 23:10

Eric Martinez