Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Captcha iOS not working #591

Open
Nineswiss opened this Issue · 0 comments

1 participant

@Nineswiss

I've tried everything but keep getting "BAD_CAPTCHA". I know I'm logged in and my cookie is set, but no matter what I do I get that error. My latest code (I'm getting a new captcha and displaying the image):

-(void)getCaptcha
{

NSString *urlString = [NSString stringWithFormat:@"http://www.reddit.com/api/new_captcha"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

[request setHTTPMethod:@"POST"];

NSString *httpBody = [NSString stringWithFormat:@"api_type=json"];
[request setHTTPBody:[httpBody dataUsingEncoding:NSASCIIStringEncoding]];


NSURLResponse* response;
NSError* error = nil;
NSData* result = [NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response
                                                   error:&error];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];

captcha = [NSString stringWithString:[[[json objectForKey:@"json"] objectForKey:@"data"]objectForKey:@"iden"]];
NSLog(@"RESPONSE: %@",captcha);


NSString *captchaPNG = [NSString stringWithFormat:@"http://www.reddit.com/captcha/%@.png",captcha];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:captchaPNG]];
UIImage *image = [UIImage imageWithData:imageData];
captchaPic.image = image;

}
Then when the user subits their captcha input (from 'captchaText.text'):

captchaResult = captchaText.text;
NSLog(@"TEXT: %@",captchaResult);

NSString *urlString = [NSString stringWithFormat:@"http://www.reddit.com/api/submit"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

[request setHTTPMethod:@"POST"];
NSString*httpBody = [NSString stringWithFormat:@"captcha=%@&iden=%@&uh=%@&kind=link&url=%@&sr=%@&title=%@&r=%@&api_type=json",captcha,captchaResult,modhash,@"www.google.com",@"test",@"Google.com",@"test"];
[request setHTTPBody:[httpBody dataUsingEncoding:NSASCIIStringEncoding]];

NSURLResponse* response;
NSError* error = nil;
NSData* result = [NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response
                                                   error:&error];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];

NSDictionary *responseJSON = [json valueForKey:@"json"];
NSLog(@"RESPONSE: %@",responseJSON);

}

This results in the "BAD_CAPTCHA" error. It makes no sense. Also before I used this code I would try and submit with out a captcha it gives me an iden but if I try to resubmit with that it get the same error. It's driving me crazy!
Anyone able to help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.