I'm attempting to post an image onto the twitter api, v1.1 I've tried just about all the example out there, and nothing seems to be able to post it. include Posting images to twitter in Node.js using Oauth
I'm using the oauth library mentioned there, and I also had jsOauth, which I thought I'd give a shot according to https://gist.github.com/lukaszkorecki/1038408
Nothing has worked, and at this point I'm starting to lose hope on whether I can even do this.
function postStatusWithMedia(status, file) {
var err = new Object();
if(fs.existsSync(file) === false) {
err.message = "File not found :(";
parseTwitterError(err);
} else {
var oauth = OAuth(options = {
"consumerKey": consumer_key,
"consumerSecret": consumer_secret,
"accessTokenKey": access_token,
"accessTokenSecret": access_token_secret
});
callbacks = {
onSuccess : function() {
console.log('upload worked!')
},
onFailure : function() {
console.log('upload failed!');
console.dir(arguments);
}
},
uploadData = {
'status' : status,
'media' : Base64.encode(fs.readFileSync(file))
};
oauth.post('https://api.twitter.com/1.1/statuses/update_with_media.json',uploadData, callbacks.onSuccess, callbacks.onFailure);
return false;
}
}
If it can't be done, can you please explain why? Otherwise, anything that could lead me to the right direction would be great.
I ended up using
request
andnode-form-data
to manually construct a multipart/form-data request and send it with the status request,utf8
was for encoding the status into UTF-8, not doing so caused issues with '<3' and other characters.I have not tested these code.Its from my colleague.sure the code is working. Perhaps this will help.
//twitter_update_with_media.js
next file //upload_to_twitter.js