【问题标题】:Using Facebook Access Token server- side Authentication使用 Facebook 访问令牌服务器端身份验证
【发布时间】:2013-06-24 06:06:27
【问题描述】:

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

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

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

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

我已经尝试了很多 Facebook 查询来查询图形 API,但我发现了这些查询,但都没有达到我的预期。可以举个例子吗?

【问题讨论】:

标签: iphone facebook-graph-api ios6


【解决方案1】:

我正在通过这个获取 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登录进入应用程序,您可以通过this sample

希望对您有所帮助...

【讨论】:

猜你喜欢
  • 2012-06-05
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多