Update Statuses With Media
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
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 does that work for you?
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.
@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 @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?
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 Thank you so much for your comment. Its worked for me.
Any updates on this issue ??
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)
})