使用Facebook Access令牌服务器端身份验证

iCo*_*777 1 iphone facebook-graph-api ios6

我正在开发基于与服务器通信的iPhone应用程序,我想使用Facebook身份验证机制.

基本上,我认为它应该像这样工作:

  1. 在我的iPhone应用程序中,用户使用他的电子邮件和密码登录Facebook.
  2. 用户可以访问相关Facebook应用程序的数据.
  3. 成功登录后,我的iPhone应用程序会收到访问令牌.
  4. 在与我的服务器的进一步通信中,我的iPhone应用程序应该使用收到的Facebook访问令牌(例如:在查询中).当我的服务器从iPhone应用程序收到一些带有访问令牌的查询时,它应该询问Facebook这个令牌是否有效(并且对于who),如果是,服务器应该假定用户通过Facebook进行身份验证.

我的问题是:如果给定的访问令牌有效,服务器应该如何询问Facebook?我想我应该以某种方式检查令牌是否对我的Facebook应用程序有效.

我已经尝试了很多Facebook查询到图形API,我发现,但没有任何工作像我预期的那样.你能举个例子吗?

Bha*_*anu 8

我通过这个获得accessToken

NSString * accessToken = [[FBSession activeSession] accessToken];
Run Code Online (Sandbox Code Playgroud)

然后用户可以通过实现此方法获取用户的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];
        }
  }
Run Code Online (Sandbox Code Playgroud)

要通过Facebook登录进入应用程序,您可以浏览此示例

希望对你有帮助...