node-twitter icon indicating copy to clipboard operation
node-twitter copied to clipboard

Image Upload Issue

Open technotip opened this issue 9 years ago • 4 comments

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

technotip avatar Mar 21 '15 08:03 technotip

I can't get media uploading to work even on localhost. I make a call and it never comes back

mbellmbell avatar Apr 09 '15 04:04 mbellmbell

Did you resolve your issue ?

afaucogney avatar Apr 06 '16 10:04 afaucogney

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; }

aistore2030 avatar May 03 '16 15:05 aistore2030

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.

jaranta avatar Jun 30 '17 14:06 jaranta