Search code examples
objective-cpostnsurlconnectionnsdatansurlrequest

Get JSON from Snapchat/Snaphax API in objective C


I"m trying to post data to a server from objective C and I am trying to get a JSON returned in it.

I am looking at the Snaphax API for PHP and Snaphaxpy API and trying to rewrite it from PHP into Objective C. The links for the code are: https://github.com/tlack/snaphax https://github.com/jasonanovak/snaphaxpy/blob/master/snaphaxpy.py

I'm also especially looking at: http://adamcaudill.com/2012/06/16/snapchat-api-and-security/ but apparently this is outdated

My code is:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://feelinsonice.appspot.com/ph/login"]];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"******testusername******" forHTTPHeaderField:@"username"];
    [request addValue:@"*********" forHTTPHeaderField:@"password"];
    [request addValue:@"M02cnQ51Ji97vwT4" forHTTPHeaderField:@"blob_enc_key"];
    [request addValue:@"false" forHTTPHeaderField:@"debug"];
    [request addValue:@"iEk21fuwZApXlz93750dmW22pw389dPwOk" forHTTPHeaderField:@"secret"];
    [request addValue:@"m198sOkJEn37DjqZ32lpRu76xmw288xSQ9" forHTTPHeaderField:@"static_token"];
    [request addValue:@"Snaphax 4.0.1 (iPad; iPhone OS 6.0; en_US)" forHTTPHeaderField:@"user_agent"];
    [request addValue:@"930cf95a6731dc986ef3bceef6abbaf420e94d8d197dca87b9b47314d8c51b3b" forHTTPHeaderField:@"req_token"];
    [request addValue:@"1355776346532" forHTTPHeaderField:@"timestamp"];


    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];        
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    NSLog(@"%@", dataString);

I haven't included a real username/password. How come, however, does this not work as I have literally copied everything I have found and implemented in a new language...

Am I not POSTing the data correctly? I tried using ASIHTTPRequest but I couldn't get that working either...

Any suggestions or ideas based on experience??


Solution

  • Check my answer here: how can I use NSURLConnection Asynchronously?

    You can send a POST request by using

    HTTPCachedController *ctrl = [[[HTTPCachedController alloc] initWithRequestType:1 andDelegate:self] autorelease];
    [ctrl postRequestToURL:@"https://feelinsonice.appspot.com/ph/login" withData:@"username=user1&password=pass& ... "];
    

    Source code of the HTTPCachedController can be found here: HTTPCachedController