7

So I'm currently using Facebook to login to my iOS app, and the server administrator recently added security authentication by requiring a Facebook access token along with making all connections go via https. Here's the code I've tried running, but I've been getting a server response error of 500 (internal server error) no matter what I do. Any ideas?

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@"https://XXXXXXXXXXXXXXXX/users.json"]];

NSDictionary *requestData = [[NSDictionary alloc] initWithObjectsAndKeys:
                     userID, @"facebook_id",
                     FBAccessToken, @"fb_token",
                     userName, @"name",
                     nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Julian Coltea
  • 3,599
  • 10
  • 26
  • 32
  • 500 errors usually mean the problem lies with server and not the client. http://pcsupport.about.com/od/findbyerrormessage/a/500servererror.htm – Mike D Mar 12 '13 at 02:37
  • Not a lot to go on here. What's in the response body? – Jeffery Thomas Mar 12 '13 at 02:38
  • @MikeD It was working fine before they added the facebook security and both the people working on the web and android versions of the app were able to communicate with the server. – Julian Coltea Mar 12 '13 at 02:48
  • @JefferyThomas Could you give me the code snippet for the response body and I'll post what I get back? – Julian Coltea Mar 12 '13 at 02:48

2 Answers2

3

Here is how you log the response body.

@property (strong, nonatomic) NSMutableData *responseData;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.responseData = [NSMutableData data];
    …
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.responseData appendData:data];
    …
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"response data - %@", [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]);
    …
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
1

You also need to conform to the NSURLConnectionDelagate protocol.