我没有使用图形api得到facebook的朋友列表

时间:2014-05-17 05:50:19

标签: ios facebook-graph-api ios7 facebook-ios-sdk

我想在不使用“FBFriendPickerViewController”的情况下获取登录用户的好友列表。所以我使用Graph API这样做,但它没有给我朋友列表。我可以成功登录,也可以获取登录用户的信息。我已关注此链接https://developers.facebook.com/docs/graph-api/reference/v2.0/user/friendlists

到目前为止,我已尝试过此代码段代码

-(IBAction)loginWithFacebook:(id)sender {

    if (FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state ==FBSessionStateOpenTokenExtended) {
        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        [FBSession.activeSession closeAndClearTokenInformation];
    } 
    else {
        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions",@"manage_friendlists",@"public_profile",@"user_friends"]
                                       defaultAudience:FBSessionDefaultAudienceEveryone
                                          allowLoginUI:YES
                                     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                         [self sessionStateChanged:session state:status error:error];
                                     }];
    }
}


-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
    // If the session was opened successfully
    if (!error && state == FBSessionStateOpen){
        NSLog(@"Session opened");
        // Show the user the logged-in UI
        [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {

            NSLog(@"%@",user);
            NSLog(@"email::: %@",[user objectForKey:@"email"]);
        }];
        return;
    }
    if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
        // If the session is closed
        NSLog(@"Session closed");
    }

    // Handle errors
    if (error){
        NSLog(@"Error");
        NSString *alertText;
        NSString *alertTitle;
        // If the error requires people using an app to make an action outside of the app in order to recover
        if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
            alertTitle = @"Something went wrong";
            alertText = [FBErrorUtility userMessageForError:error];
            [self showMessage:alertText withTitle:alertTitle];
        } else {

            // If the user cancelled login, do nothing
            if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
                NSLog(@"User cancelled login");

                // Handle session closures that happen outside of the app
            } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
                alertTitle = @"Session Error";
                alertText = @"Your current session is no longer valid. Please log in again.";
                [self showMessage:alertText withTitle:alertTitle];

            } else {
                //Get more error information from the error
                NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];

                // Show the user an error message
                alertTitle = @"Something went wrong";
                alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
                [self showMessage:alertText withTitle:alertTitle];
            }
        }
        // Clear this token
        [FBSession.activeSession closeAndClearTokenInformation];
    }
}

登录后我尝试获取我写过的好友列表

- (IBAction)fetchFrinds:(id)sender {

    [FBRequestConnection startWithGraphPath:@"/me/friendlists"
                                 parameters:@{@"fields": @"id,name"}
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {

                              NSLog(@"%@",result);
                          }];
}

2 个答案:

答案 0 :(得分:3)

根据Facebook Graph API 2.0 docs on Friendlists

  

/ {用户ID} /好友列表

     

一个人的'friend lists' - 这些是朋友的分组,例如“熟人”或“关闭朋友”,或其他可能已创建的朋友。他们没有引用一个人拥有的朋友列表,而是通过/{user-id}/friends边缘访问。

因此,根据您当前的请求,您将获得朋友列表而不是朋友列表。


要获取朋友列表,您需要参考:


注意:
Facebook似乎改变了它的实施 您无法再获得整个​​朋友列表 现在......列表将仅限于那些也碰巧使用您的应用的朋友。

引用Facebook Graph API 2.0 doc:

  

<强>权限

     
      
  • 查看当前此人的朋友需要具有 user_friends 权限的用户访问令牌。
  •   
  • 这只会返回使用(通过Facebook登录)申请的应用的任何朋友。
  •   

答案 1 :(得分:2)

如果通过friendslist表示已登录用户的朋友列表,则图表路径为me/friends。打开具有读取权限的有效FBSession后,这样的内容对我有用。

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,picture",@"fields",nil];

[FBRequestConnection startWithGraphPath:@"me/friends"
                             parameters:params
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if(error == nil) {
                              FBGraphObject *response = (FBGraphObject*)result;
                              NSLog(@"Friends: %@",[response objectForKey:@"data"]);
                          }
                      }];

请注意,FBRequestConnection成功请求的结果为FBGraphObject,其中包含为密钥'data'返回的所需信息。
在为朋友列表FBSession之前,您可以打开一个有basic_info读取权限的有效FBRequestConnection
希望这有帮助