node-twitter
node-twitter copied to clipboard
Image Upload Issue
Image upload not working on the actual domain .. Same code works fine in localhost / 127.0.0.1, but starts throwing 400 error while trying image upload on the actual server. Everything else works fine(like fetching profile, updating profile pics etc), except this :(
Here is my code.
authoVariable.post('media/upload',
{'media': fs.readFileSync(req.files[file].path, { encoding: 'base64' })},
function(error, params, response){
if(error) console.log("media/upload error 1 "+error);
});
am getting error media/upload error 1 Error: Status Code: 400
I can't get media uploading to work even on localhost. I make a call and it never comes back
Did you resolve your issue ?
I did following way it work
global.autoposttotwitter = function () { console.log('auto post started'); logstep("auto post started","LINE 61"," APP/social media.JS");
var Twitter = require('twitter');
var client = new Twitter({
consumer_key:
consumer_secret:
access_token_key: ' ,
access_token_secret:
});
client.post('statuses/update', {status: '#sakshsocial managed by Sakshamapp Social Media Team we managed twitter in just 5$ per month contact https://www.sakshamappinternational.com/ '}, function(error, tweet, response){ // if(error) throw error; /// console.log(tweet); // Tweet body. //console.log(response); // Raw response object. });
var file=getRandomFile('./socialmediaimage'); logstep("auto post started"+file,"LINE 61"," APP/social media.JS");
// res.send(items); // Load your image var data = require('fs').readFileSync(file );
// Make post request on media endpoint. Pass file data as media parameter client.post('media/upload', {media: data}, function(error, media, response){ // read a random files
if (!error) {
// If successful, a media object will be returned.
// console.log(media);
// Lets tweet it
var status = {
status: '#quote #inspiration #quotes #sakshsocial ',
media_ids: media.media_id_string // Pass the media id string
}
client.post('statuses/update', status, function(error, tweet, response){
if (!error) {
//console.log(tweet);
}
});
}
logstep("auto post end"+file,"LINE 61"," APP/social media.JS");
});
}
function getRandomFile(dir) { var items=getFiles (dir);
var v=Math.floor(Math.random()*(items.length-2));
var file = items[v];
//console.log(v); //c/onsole.log(file);
return file; }
function getFiles (dir, files_){ fs=require('fs'); files_ = files_ || []; var files = fs.readdirSync(dir); for (var i in files){ var name = dir + '/' + files[i]; if (fs.statSync(name).isDirectory()){ getFiles(name, files_); } else { files_.push(name); } } return files_; }
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; }
I'm also running into this. My code is essentially identical to the example given (& what @sakshamapp is using). I also get a 400 error. It seems the response contains a more specific error: 'media type unrecognized.'
Not sure how to troubleshoot further.