Upload media to upload.twitter - error "code":38,"message":"media parameter is missing."


#1

Hi,

I trying to use Twitter API to tweet the message with image (NodeJS application). But always I have the following error:

{ statusCode: 400,
  data: '{"errors":[{"code":38,"message":"media parameter is missing."}]}' }

My code is the following:

twitter.oa.post('https://upload.twitter.com/1.1/media/upload.json',
    twitter.access_token,
    twitter.access_token_secret,
    {media_data: imageBase64},
    function(e, data) {
        if (e) {
          console.error(e);
        }else {
          try{
            data = JSON.parse(data);
          }catch (e){
            console.error("Error Json : " + e);
          }
          console.log(data.media_id);

          twitter.post('statuses/update', {status:body, media_ids:[data.media_id_string]}, "", function (e, data, res){
            if (e) {
              cb(e);
            }else {
              cb(null, data);
            }
          });
        }
    }
);

I’m also tried this one:

twitter.oa.post('https://upload.twitter.com/1.1/media/upload.json',
    twitter.access_token,
    twitter.access_token_secret,
    {media_data: '@' + imageBase64 + ';type="image/png";filename="93993d40-89ef-11e4-9521-254a3df0c166.png"'},
    function(e, data) {
        if (e) {
          console.error(e);
        }else {
          try{
            data = JSON.parse(data);
          }catch (e){
            console.error("Error Json : " + e);
          }
          console.log(data.media_id);

          twitter.post('statuses/update', {status:body, media_ids:[data.media_id_string]}, "", function (e, data, res){
            if (e) {
              cb(e);
            }else {
              cb(null, data);
            }
          });
        }
    }
);

As you can see from the code I upload the image in base64. In the second code I hardcoded the image type, the uploaded image have the png format.

What I’m missed? Thank you in advance.


#2

Any suggestions?


#3

I have the same issue. How did you fix this? Thanks in advance…


#4
function sendMessageWithImage(urlToTheImage, userAuth, body, cb) {    
    var statusMessage = body,
        OAuth = require('oauth').OAuth,
        twitterConfig = require('../../config/config').twitter;
    
    var auth = new OAuth('https://twitter.com/oauth/request_token',
        'https://twitter.com/oauth/access_token',
        twitterConfig.consumerKey,
        twitterConfig.consumerSecret,
        '1.0A',
        null,
        'HMAC-SHA1'
    );

    request({
        url: urlToTheImage,
        encoding: null
    }, function(error, res, body) {
        if (!error && res.statusCode == 200) {
            var image = body.toString('base64');

            auth.post("https://upload.twitter.com/1.1/media/upload.json",
                userAuth.access_token,
                userAuth.token_secret, {
                    media: image
                },
                function(error, uploadResult) {
                    if (!error) {
                        var data = JSON.parse(uploadResult);

                        auth.post("https://api.twitter.com/1.1/statuses/update.json",
                            userAuth.access_token,
                            userAuth.token_secret, {
                                status: statusMessage,
                                'media_ids': data.media_id_string
                            },
                            function(error, data) {
                                if (error) {
                                    cb(error);
                                } else {
                                    cb(null, data);
                                }
                            }
                        );
                    } else {
                        cb(error);
                    }
                }
            );
        } else {
            cb(new Error('Cannot download image'));
        }
    });
}