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?

4

1 回答 1

8

我通过这个获得 accessToken

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

然后用户可以通过实现这个方法来获取用户的 Facebook 数据:

 - (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];
        }
  }

对于通过 Facebook 登录来登录应用程序,您可以查看此示例

希望对你有帮助...

于 2013-06-24T06:28:54.540 回答