1

I am trying to upload an image to twitter account using twitter's media upload api. I am using node-twitter module for nodejs.

This is the code i am using

fs.writeFile("out.png", base64String, 'base64', function (err) {
                console.log(err);                
            });

var data = fs.readFileSync("out.png");

// Make post request on media endpoint. Pass file data as media parameter
client.post('media/upload', {media: data}, function(error, media, response){

  if (!error) {

    // If successful, a media object will be returned.
    console.log(media);

    // Lets tweet it
    var status = {
      status: 'I am a tweet',
      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);
      }
    });

  }
  else{
     console.log(error);
  }
});
Skyyy
  • 1,539
  • 2
  • 23
  • 60

1 Answers1

0

Response code 400 stands for "Bad request" therefor I assume that format of your post is wrong, most-likely the way you load your image. Just try to remove that

fs.writeFile("out.png", base64String, 'base64', function (err) {
                console.log(err);                
            });

Because it is trying to write file rather than read it from your disk

Tomas
  • 1,131
  • 2
  • 12
  • 25
  • I have a base64 string so i save it in png so that i can use that png to upload to twiter – Skyyy Jan 08 '16 at 16:15
  • You could try your function with a regular image in png format just to see if its actually problem with the Post method or is it a problem with your image – Tomas Jan 08 '16 at 16:46
  • Print following output in the console when using regular image ` { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }` – Skyyy Jan 08 '16 at 17:34
  • try adding more information to your console.log() so you would know where this is coming from such as console.log('this is coming from error'+error); and console.log('successful tweet'+tweet); but it seems that you're getting to error block, which means theres something wrong with your request. Make sure you have correct credentials and api key for your twitter account – Tomas Jan 08 '16 at 17:39
  • Uploading a regular image worked(Previously it was not uploading due to slow internet connection). How can i make it work with base64 encoded images. – Skyyy Jan 08 '16 at 18:04
  • I wouldn't know as I have never worked with these technologies, but I assume there must be a way to save your picture in a regular format and then upload it to twitter? After short research I found that [this thread](http://stackoverflow.com/questions/20267939/nodejs-write-base64-image-file) might help you to solve the problem that you're after, best of luck! – Tomas Jan 11 '16 at 09:34