So I've solved the problem, though not to my satisfaction. A working version of what I posted this weekend is below. There were a number of things that I was doing wrong.
- As noted by gotnull above, I had fat-fingered
method
asmethods
. - For whatever reason request's cookie jar was not working for me. I had to handle the cookie manually as illustrated by the code.
api.reddit.com
doesn't handlehttps
so I had to send the request to post a comment tohttps://en.reddit.com/
I'm still unhappy with the way that this works, because it limits the length of my message by having it in the URI. From what I understand I need to have the message in the POSTDATA but I haven't been able to coax request into doing that for me.
My initial problem is solved, but I'll need to open a second question for this when I re-visit the bot.
var request = require('request')
, argv = require('yargs').argv
, modhash
, cookie;
function login () {
var options = {
url : 'https://ssl.reddit.com/api/login?api_type=json&user=' + argv.user + '&passwd=' + argv.pass + '&rem=True',
headers : {
'User-Agent' : 'fooBot/0.1 by USERNAME',
},
method : 'POST'
};
request(options, function (err, res, body) {
if (err) {
console.log('LOGIN ERROR:');
console.log(err.json.errors);
return;
} else {
var parsedBody = JSON.parse(body);
modhash = parsedBody.json.data.modhash;
cookie = parsedBody.json.data.cookie;
}
});
}
function postComment (parentId) {
var text = 'message message message.'
, options = {
url : 'https://en.reddit.com/api/comment?api_type=json&text=' + encodeURIComponent(text) + '&thing_id=' + parentId,
headers : {
'User-Agent' : 'fooBot/0.1 by USERNAME',
'X-Modhash' : modhash,
'Cookie' : 'reddit_session=' + encodeURIComponent(cookie)
},
method : 'POST' };
request(options, function (err, res, body) {
if (err) {
console.log('COMMENT POST ERROR:');
console.log(err.stack);
return;
} else {
console.log('// ------ //');
console.log(body);
console.log('// ------ //');
}
});
}