最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter API - upload an image - Stack Overflow

programmeradmin2浏览0评论

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:'.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 ?

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./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 ?

Share Improve this question edited Aug 21, 2015 at 9:34 jolyonruss 1,8402 gold badges20 silver badges35 bronze badges asked Aug 12, 2015 at 23:17 URL87URL87 11k36 gold badges111 silver badges177 bronze badges 3
  • 1 From POST media/upload : Ensure your POST is a multipart/form-data request. Did you do that? (I haven't used twitter's api, but it seems that it might be that) – Eric Martinez Commented Aug 15, 2015 at 20:19
  • 1 Checked and found - Content-Type:multipart/form-data – URL87 Commented Aug 15, 2015 at 21:25
  • 1 Why don't you use the current API hook instead of the deprecated one? This endpoint has been DEPRECATED. Please use POST statuses/update for uploading one or more media entities. – ChrJantz Commented Aug 21, 2015 at 12:38
Add a ment  | 

1 Answer 1

Reset to default 8 +50

I couldn't post this as a ment, 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./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.

发布评论

评论列表(0)

  1. 暂无评论