【问题标题】:FBSession.session.accessTokenData.userID always nilFBSession.session.accessTokenData.userID 始终为零
【发布时间】:2015-02-09 06:28:26
【问题描述】:

一直在使用 FBConnect 选择朋友,但 FBSession 的 accessTokenData 从来没有启动会话的用户的用户 ID(他们的登录演示中也不存在)。 FBFriendPickerViewController 可以很好地返回带有 ID 的朋友用户,但我没有看到有关当前用户的任何信息。是否可以通过FB活动会话、accessTokenData或其他方式获取当前用户的Id?

编辑: 是否可以从该调用中获取用户的 Id?

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",  @"user_friends"]

一步完成会更好。

【问题讨论】:

    标签: objective-c fbconnect


    【解决方案1】:

    您必须向“/me”请求获取“user_id”,因为 access_token 属性已加密,您无法通过这种方式访问​​它。

    【讨论】:

    • 谢谢,意识到我需要链接完成处理程序。不确定它是否非常直观。
    【解决方案2】:

    这是我最终做的:

    if (!FBSession.activeSession.isOpen) {
    // if the session is closed, then we open it here, and establish a handler for state changes
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState state,
                                                      NSError *error) {
                                    if (error) {
                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                          message:error.localizedDescription
                                                                                         delegate:nil
                                                                                cancelButtonTitle:@"OK"
                                                                                otherButtonTitles:nil];
                                      [alertView show];
                                    }
                                    else if (session.isOpen) {
    
                                      if ([session.declinedPermissions containsObject:@"user_friends"]) {
                                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"friends"
                                                                                            message:@"Must allow accessing friends"
                                                                                           delegate:nil
                                                                                  cancelButtonTitle:@"OK"
                                                                                  otherButtonTitles:nil];
                                        [alertView show];
                                      }
                                      else {
                                        [[FBRequest requestForMe] startWithCompletionHandler:
                                         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
    
                                           if (error) {
                                             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                                 message:error.localizedDescription
                                                                                                delegate:nil
                                                                                       cancelButtonTitle:@"OK"
                                                                                       otherButtonTitles:nil];
                                             [alertView show];
                                           }
                                           else {
                                             NSLog(@"name %@  id %@", user.first_name, user.objectID);
    
                                             [[FBRequest requestForMyFriends] startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                                           NSDictionary* result,
                                                                                           NSError *error) {
                                               NSArray* friends = [result objectForKey:@"data"];
                                               NSLog(@"Found: %i friends", friends.count);
    
                                               for (NSDictionary<FBGraphUser>* friend in friends) {
                                                 NSLog(@"I have a friend named %@ with id %@", friend.name, friend.objectID);
                                               }
                                             }];
                                           }
                                         }];
                                      }
                                    }
                                  }];
    

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 2012-01-16
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      相关资源
      最近更新 更多