Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created July 29, 2010 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CastIrony/497538 to your computer and use it in GitHub Desktop.
Save CastIrony/497538 to your computer and use it in GitHub Desktop.
-(void)authenticateWithEmail:(NSString*)email password:(NSString*)password retries:(int)retries andThen:(BasicBlock)work
{
NSString* urlString = @"https://www.google.com/accounts/ClientLogin";
NSMutableDictionary* requestParameters = [NSMutableDictionary dictionary];
[requestParameters setObject:email forKey:@"Email"];
[requestParameters setObject:password forKey:@"Passwd"];
[NSURLConnection postToUrlString:urlString withParameters:requestParameters completionBlock:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSString* dataString = [NSString stringWithUTF8String:[data bytes]];
if([dataString hasPrefix:@"SID="])
{
NSString* sid = [[[dataString componentsSeparatedByString:@"\n"] objectAtIndex:0] substringFromIndex:4];
[NSHTTPCookieStorage setCookieWithName:@"SID" value:sid origin:urlString];
RunOnMainThread(work);
}
else if(retries > 0)
{
NSLog(@"Authentication failed, %i retries left", retries);
RunAfterDelay(0.2, ^{ [self authenticateWithEmail:email password:password retries:(retries - 1) andThen:work]; });
}
else
{
NSLog(@"Authentication failed for good");
}
}];
}
@lilyball
Copy link

-(void)authenticateWithEmail:(NSString *)email password:(NSString *)password retries:(int)retries andThen:(BasicBlock)work {
NSString *urlString = @"https://www.google.com/accounts/ClientLogin";

NSMutableDictionary* requestParameters = [NSMutableDictionary dictionary];

[requestParameters setObject:email    forKey:@"Email"];
[requestParameters setObject:password forKey:@"Passwd"];

[NSURLConnection postToUrlString:urlString withParameters:requestParameters completionBlock:^(NSData *data, NSURLResponse* response, NSError *error) {
    NSString *dataString = [NSString stringWithUTF8String:[data bytes]];

    if ([dataString hasPrefix:@"SID="]) {
        NSString *sid = [[[dataString componentsSeparatedByString:@"\n"] objectAtIndex:0] substringFromIndex:4];
        [NSHTTPCookieStorage setCookieWithName:@"SID" value:sid origin:urlString];
        RunOnMainThread(work);
    } else if (retries > 0) {
        NSLog(@"Authentication failed, %i retries left", retries);
        RunAfterDelay(0.2, ^{ [self authenticateWithEmail:email password:password retries:(retries - 1) andThen:work]; });
    } else {
        NSLog(@"Authentication failed for good");
    }
}];

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment