1

I'm developing iPhone application, that is based on communication with server, and I want to use Facebook authentication mechanisms.

Basically, I think it should work like this:

  1. In my iPhone app, user logs in to Facebook, using his email and password.
  2. User allows access to his data for related Facebook application.
  3. My iPhone app receives access token, after successful log in.
  4. In further communication with my server, my iPhone application should using received Facebook access token (for example: in queries).When my server receives some query from iPhone app, with access token, it should ask Facebook that this token is valid (and for who), and if yes, server should assume that user is authenticated with Facebook.

My question is: how the server should ask Facebook if given access token is valid? I think I should somehow check if the token is valid for my Facebook app.

I've tried many Facebook queries to graph API, that I've found, but nothing worked as I expected. Can you provide me some example?

3

1 Answer 1

8

I am getting accessToken through this

NSString * accessToken = [[FBSession activeSession] accessToken];

then user can get user's Facebook data by implementing this method :

 - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
    {
        switch (state)
        {            ///  ****************   IT GIVES THIS ACCESS TOKEN   *****************///////////////////////// 
            case FBSessionStateOpen:
            {
                      // https://graph.facebook.com/me?access_token=%@
                 accessToken = [[FBSession activeSession] accessToken];
                NSLog(@"accessToken: %@",accessToken);
                NSString *urlList = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",accessToken];
                NSURL *url1 = [NSURL URLWithString:urlList];
                NSURLRequest *urlReqst = [[NSURLRequest alloc] initWithURL:url1];
                NSData *dataConnection = [NSURLConnection sendSynchronousRequest:urlReqst
                                                               returningResponse:nil
                                                                           error:nil];
                NSString *jsonData = [[NSString alloc] initWithData:dataConnection
                                                           encoding:NSUTF8StringEncoding];
                NSDictionary *jsonDic = [jsonData JSONValue];

                self.appid = [jsonDic valueForKey:@"id"];
                self.appusername = [jsonDic valueForKey:@"username"];
                self.appfirst_name = [jsonDic valueForKey:@"first_name"];
                self.applast_name = [jsonDic valueForKey:@"last_name"];
                self.appname = [jsonDic valueForKey:@"name"];
                self.appemailId = [jsonDic valueForKey:@"email"];

                [self LoginAPI];
                    if ([self.navigation.presentedViewController isKindOfClass:[LoginPage class]]) {
                    [self.navigation.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                }
            }
                break;
            case FBSessionStateClosed:
            case FBSessionStateClosedLoginFailed:
                [self.navigation popToRootViewControllerAnimated:NO];
                            [self showLoginView];
                [self fbDidLogout];

                break;
                    default:
                break;
        }
        if (error)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
  }

And for getting login into app through Facebook login, you may go through this sample

Hope this will help you...

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.