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



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

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

  1. 在我的iPhone应用程序中,用户使用电子邮件和密码登录Facebook
  2. 用户允许访问相关Facebook应用程序的数据
  3. 成功登录后,我的iPhone应用程序会收到访问令牌
  4. 在与我的服务器的进一步通信中,我的iPhone应用程序应该使用收到的Facebook访问令牌(例如:在查询中)。当我的服务器收到来自iPhone应用程序的带有访问令牌的查询时,它应该询问Facebook该令牌是有效的(以及对谁有效),如果是,服务器应该假设用户通过了Facebook的身份验证

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

我已经尝试了许多Facebook查询来绘制API,我发现了,但没有像我预期的那样工作。你能给我举个例子吗?

我通过这个获得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登录进入应用程序,您可以通过以下示例

希望这能帮助你。。。

相关内容

最新更新