Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reddit API in php returns bad captcha for submitting story

Using php for Reddit api for submitting a story returns bad captcha as error. I am able to login using the api and get usermod and captcha perfectly using api. Ideally if the reddit_session cookie is passed it should post and not return bad captcha can someone shed me some light on this..

reference link: https://github.com/reddit/reddit/wiki/API

<?php
$user = "";
$passwd = "";
$url = "http://www.reddit.com/api/login/".$user;

$r = new HttpRequest($url, HttpRequest::METH_POST);
$r->addPostFields(array('api_type' => 'json', 'user' => $user, 'passwd' => $passwd));

try {
    $send = $r->send();
    $userinfo = $send->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

$arr = json_decode($userinfo,true);

$modhash = $arr['json']['data']['modhash'];
$reddit_session = $arr['json']['data']['cookie'];

$post = array('uh'=>$modhash,
               'kind'=>'link',
                'url'=>'yourlink.com',
                'sr'=>'funny',
                'title'=>'omog-asdfasf',
                'id'=>'newlink',
                'r'=>'funnyier',                
                'renderstyle'=> 'html'              
                );


$url = "http://www.reddit.com/api/submit";

// Upvote RoboHobo's comment :)
// Add user cookie data
$r->addCookies(array("reddit_session" => $reddit_session));
// Set URL to vote
$r->setUrl($url);
// Add vote information, found at http://wiki.github.com/talklittle/reddit-is-fun/api-all-functions
$r->setPostFields($post);
// Send request blindly


try {
    $userinfo = $r->send();
} catch (HttpException $ex) {
    echo $ex;   
}
pre($userinfo);
exit;

function pre($r){
echo "<pre />";
print_r($r);
}
?>
like image 682
Ramji Avatar asked Sep 15 '11 21:09

Ramji


2 Answers

For anyone else that stumbled onto this question lately and is still having that issue:

The above issue was fixed and works correctly however if you created a new account for your reddit bot and try to submit a story you will recieve a bad_captcha error. New accounts have to submit captchas until they gain a certain amount of karma so this is the error you are seeing. Try the request with an older account and this should solve your problem.

like image 96
Jerrod Avatar answered Sep 24 '22 11:09

Jerrod


From what I can tell, at the moment CAPTCHA is broken in the Reddit API. They originally were using an outdated PyCAPTCHA and were migrating to reCAPTCHA. Since then, there has been an issue with using api_type:json which has a work around and someone on github is currently working it. He also offered an explanation/solution:

Quite simply, the json (though not jquery) result should contain the captcha_id when a >captcha is required. By captcha_id I mean the part to complete a url like the followng: >http://www.reddit.com/captcha/(captcha_id).png

The use case I encountered is when trying to submit a story via the api using >api_type:json. I am nicely notified that my non-existent captcha is incorrect, however, I >then have to make a request to http://www.reddit.com/api/new_captcha in order to get the >captcha_id. This last round trip seems unnecessary.

like image 30
Jeremy Harris Avatar answered Sep 24 '22 11:09

Jeremy Harris