twit icon indicating copy to clipboard operation
twit copied to clipboard

Update Statuses With Media

Open nambad opened this issue 12 years ago • 9 comments

Hi,

Can someone please give me an example of how to get this working with twit?

https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media

nambad avatar Jan 29 '14 10:01 nambad

You need to send the raw bytes or base64 encoded string e.g.:

var fs = require('fs');
fs.readFile('yourimage.png', function(err, data) {
  var base64media = new Buffer(data).toString('base64');
  T.post('statuses/update', { 
    status: 'hello media!',
    media: [base64media]
  }, function(err, reply) {
    console.log('done')
  });
}

Hope that helps.

SargoDarya avatar Mar 04 '14 10:03 SargoDarya

@SargoDarya does that work for you?

chrisbutler avatar Mar 16 '14 23:03 chrisbutler

chrisbutler: Haven't tested it, but from the API docs and how the lib is build this should work. Be careful though as the API doesn't allow all filetypes.

SargoDarya avatar Mar 17 '14 07:03 SargoDarya

@nambad @SargoDarya the biggest gotcha is that the request needs to be sending multipart/form-data which twit doesn't seem capable of

i ended up using the npm request package to do the call manually using form.append('media[]')

chrisbutler avatar Mar 17 '14 19:03 chrisbutler

@chrisbutler @SargoDarya Didn't Work out for me. Though I think its a critical feature that this should support.

@chrisbutler Can you please give an example on how you were able to do this?

nambad avatar Jun 23 '14 09:06 nambad

It would be awesome to see this officially supported, maybe even with the technique shown in this gist to allow for streaming the media from the web rather than disk:

https://gist.github.com/travischoma/9279105

winzig avatar Jul 29 '14 08:07 winzig

@winzig Thank you so much for your comment. Its worked for me.

dibishks avatar Aug 19 '14 12:08 dibishks

Any updates on this issue ??

uwaseem avatar Nov 06 '15 19:11 uwaseem

Hey @uwaseem @dibishks @winzig @nambad @chrisbutler @SargoDarya this is now supported in twit! npm update twit to get it. Check out the readme for examples! For the lazy:

//
// post a tweet with media
//
var b64content = fs.readFileSync('/path/to/img', { encoding: 'base64' })

// first we must post the media to Twitter
T.post('media/upload', { media_data: b64content }, function (err, data, response) {

  // now we can reference the media and post a tweet (media will attach to the tweet)
  var mediaIdStr = data.media_id_string
  var params = { status: 'loving life #nofilter', media_ids: [mediaIdStr] }

  T.post('statuses/update', params, function (err, data, response) {
    console.log(data)
  })
})

//
// post media via the chunked media upload API.
// You can then use POST statuses/update to post a tweet with the media attached as in the example above using `media_id_string`.
// Note: You can also do this yourself manually using T.post() calls if you want more fine-grained
// control over the streaming. Example: https://github.com/ttezel/twit/blob/master/tests/rest_chunked_upload.js#L20
//
var filePath = '/absolute/path/to/file.mp4'
T.postMediaChunked({ file_path: filePath }, function (err, data, response) {
  console.log(data)
})

ttezel avatar Jan 31 '16 03:01 ttezel