7

I'm trying to post images to Twitter using the Oauth module. Here is what I have:

It throws a 403 error, I know im doing something wrong with how I add the media to the post but Im just not sure where to go from here.

var https = require('https');
var OAuth= require('oauth').OAuth;
var keys = require('./twitterkeys');
var twitterer = new OAuth(
           "https://api.twitter.com/oauth/request_token",
           "https://api.twitter.com/oauth/access_token",
           keys.consumerKey,
           keys.consumerSecret,
           "1.0",
           null,
           "HMAC-SHA1"
          );


var params = {
    status : "Tiger!",
    media : [("data:" + mimeType + ";base64,") + fs.readFileSync(path,'base64')]
};

//function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback)
twitterer.post("https://upload.twitter.com/1/statuses/update_with_media.json",
           keys.token, keys.secret, params, "multipart/form-data",
           function (error, data, response2) {
           if(error){
               console.log('Error: Something is wrong.\n'+JSON.stringify(error)+'\n');

           }else{
               console.log('Twitter status updated.\n');
               console.log(response2+'\n');
           }
           });

Here is what I belive im supose to be doing but I don't know how to do that in the Node.js Oauth module. Posting image to twitter using Twitter+OAuth

5
  • first things first, do you have nodejs installed?
    – Chamilyan
    Oct 17, 2012 at 22:34
  • Yes and im able to update my twitter status using the normal statuses/update through api.twiiter.com just not with the media. Oct 17, 2012 at 22:46
  • I believe my problem attaching the form data to the post. Oct 17, 2012 at 22:47
  • Did you ever solve this? I need the same thing.
    – Dax Fohl
    Oct 28, 2012 at 13:59
  • Nope never did, i just gave up and used twitpic with their node module. Not happy about the solution but needed to move on. Oct 29, 2012 at 21:47

1 Answer 1

13

Reviewing the code, it looks like there's no multipart/form-data handling at all in the node-oauth package right now. You can still use the node-oauth function to create the authorization header, but you'll have to do the multipart stuff on your own.

There are probably third-party libraries that can help out with that, but here's how I got it to work constructed by hand.

var data = fs.readFileSync(fileName);
var oauth = new OAuth(
    'https://api.twitter.com/oauth/request_token',
    'https://api.twitter.com/oauth/access_token',
    twitterKey, twitterSecret,
    '1.0', null, 'HMAC-SHA1');

var crlf = "\r\n";
var boundary = '---------------------------10102754414578508781458777923';

var separator = '--' + boundary;
var footer = crlf + separator + '--' + crlf;
var fileHeader = 'Content-Disposition: file; name="media"; filename="' + photoName + '"';

var contents = separator + crlf
    + 'Content-Disposition: form-data; name="status"' + crlf
    + crlf
    + tweet + crlf
    + separator + crlf
    + fileHeader + crlf
    + 'Content-Type: image/jpeg' +  crlf
    + crlf;

var multipartBody = Buffer.concat([
    new Buffer(contents),
    data,
    new Buffer(footer)]);

var hostname = 'upload.twitter.com';
var authorization = oauth.authHeader(
    'https://upload.twitter.com/1/statuses/update_with_media.json',
    accessToken, tokenSecret, 'POST');

var headers = {
    'Authorization': authorization,
    'Content-Type': 'multipart/form-data; boundary=' + boundary,
    'Host': hostname,
    'Content-Length': multipartBody.length,
    'Connection': 'Keep-Alive'
};

var options = {
    host: hostname,
    port: 443,
    path: '/1/statuses/update_with_media.json',
    method: 'POST',
    headers: headers
};

var request = https.request(options);     
request.write(multipartBody);
request.end();

request.on('error', function (err) {
    console.log('Error: Something is wrong.\n'+JSON.stringify(err)+'\n');
});

request.on('response', function (response) {            
    response.setEncoding('utf8');            
    response.on('data', function (chunk) {
        console.log(chunk.toString());
    });
    response.on('end', function () {
        console.log(response.statusCode +'\n');
    });
});    
1
  • Obviously there are some improvements to be made--notably the boundary should have a random element, but this was the first thing I cobbled together to work reasonably well being completely new to JS, and our project has moved on since then so I never got a chance.
    – Dax Fohl
    Mar 25, 2014 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.