Issue with image attaching to the tweet (Image uploaded but not shown in the tweet)

nodejs
javascript

#1

Hello all,

I have a question concerning attaching uploaded media file (in my case image) to the tweet through the API (programming language is JavaScript (nodejs) ).

So the main problem is that uploaded photo does not attach to the tweet.

I use Post Media Upload enpoint to get media_id and Post Statuses Update to publish a tweet. Tweet appears with text but without an image.

So image upload does next part of the code:

    fs.readFile(__dirname + '/../../../../public/twitter_image_1.jpg', (err, data) => {
      if (err) {
        return err
      }

      const isBuffer = Buffer.isBuffer(data)
      const imageBuffer = data // data is a buffer
      const imageBase64 = data.toString('base64')

      const postData = {
        media: imageBuffer,
        // media_data: imageBase64 // also checked, the same result
      }

      return new Promise((resolve, reject) => {
        const req = request.post({
          url: 'https://upload.twitter.com/1.1/media/upload.json',
          formData: postData, // formData ensures that this is multipart/form-data request
          oauth: oauth // oauth data
        }, (err, resp, body) => {
          if (err) {
            return reject(err)
          }

          if (resp.statusCode >= 400) {
            return reject(JSON.parse(body))
          }

          resolve(JSON.parse(body))
        })

        console.log(req)
      })
    })

I received a successful response with the next data:

{
  "media_id": 1084851957179797500,
  "media_id_string": "1084851957179797504",
  "size": 138259,
  "expires_after_secs": 86400,
  "image": {
    "image_type": "image/jpeg",
    "w": 590,
    "h": 332
  }
}

Then I use media_id_string as a parameter to create a tweet:

return new Promise((resolve, reject) => {
  const req = request.post({
    url: 'https://api.twitter.com/1.1/statuses/update.json',
    formData: { // formData ensures that this is multipart/form-data request
      status: 'test message with image',
      media_ids: '1084851957179797504' //uploadMediaResult.media_id_string
    },
    oauth: oauth // oauth data
  }, (err, resp, body) => {
    if (err) {
      return reject(err)
    }

    if (resp.statusCode >= 400) {
      return reject(JSON.parse(body))
    }

    resolve(JSON.parse(body))
  })
})

And successfully received created tweet object:

{
  "created_at": "Mon Jan 14 16:38:03 +0000 2019",
  "id": "1084852154769334272",
  "id_str": "1084852154769334272",
  "text": "test message with image",
  "truncated": false,
  "entities": {
    "hashtags": [],
    "symbols": [],
    "user_mentions": [],
    "urls": []
  },
  "source": "<a href=\"https://socioconnect.io\" rel=\"nofollow\">SocioConnect-Dev</a>",
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_id_str": null,
  "in_reply_to_screen_name": null,
  "user": {
    "id": 935577900581081100,
    "id_str": "935577900581081089",
    "name": "LiubovQA",
    "screen_name": "LiubovQA",
    "location": "",
    "description": "",
    "url": null,
    "entities": {
      "description": {
        "urls": []
      }
    },
    "protected": false,
    "followers_count": 2,
    "friends_count": 45,
    "listed_count": 0,
    "created_at": "Tue Nov 28 18:35:28 +0000 2017",
    "favourites_count": 0,
    "utc_offset": null,
    "time_zone": null,
    "geo_enabled": false,
    "verified": false,
    "statuses_count": 69,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "is_translation_enabled": false,
    "profile_background_color": "F5F8FA",
    "profile_background_image_url": null,
    "profile_background_image_url_https": null,
    "profile_background_tile": false,
    "profile_image_url": "http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png",
    "profile_image_url_https": "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png",
    "profile_link_color": "1DA1F2",
    "profile_sidebar_border_color": "C0DEED",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "has_extended_profile": false,
    "default_profile": true,
    "default_profile_image": true,
    "following": false,
    "follow_request_sent": false,
    "notifications": false,
    "translator_type": "none"
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "is_quote_status": false,
  "retweet_count": 0,
  "favorite_count": 0,
  "favorited": false,
  "retweeted": false,
  "lang": "en",
  "original_id": 1084852154769334300
}

As you can see from the description above I didn’t receive any errors. But still, the image didn’t appear in a tweet.

I also tried base64 with the parameter media_data but got the same result.

Please let me know what is wrong or what I’m doing wrong.

Thank you in advance!

NOTE:


closed #2

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.