我想在不使用“FBFriendPickerViewController”的情况下获取登录用户的好友列表。所以我使用 Graph API 来执行此操作,但它没有给我 friend 列表。我可以成功登录,并且也可以获取登录用户的信息。我已点击此链接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];
}
}
现在登录后我尝试获取我写的 friend 列表
- (IBAction)fetchFrinds:(id)sender {
[FBRequestConnection startWithGraphPath:@"/me/friendlists"
parameters:@{@"fields": @"id,name"}
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
}
最佳答案
根据 Facebook Graph API 2.0 docs on Friendlists :
/{user-id}/friendlists
A person's 'friend lists' - these are groupings of friends such as "Acquaintances" or "Close Friends", or any others that may have been created. They do not refer to the list of friends that a person has, which is accessed instead through the /{user-id}/friends edge.
因此,根据您当前的请求,您将获得好友列表而不是好友列表。
获取好友列表需要引用:
注意:
Facebook 似乎已经改变了它的实现。
您无法再获取完整的好友列表。
现在...该列表将仅限于那些碰巧也使用您的应用程序的 friend 。
引用 Facebook Graph API 2.0 文档:
Permissions
- A user access token with user_friends permission is required to view the current person's friends.
- This will only return any friends who have used (via Facebook Login) the app making the request.
关于ios 无法使用 graph api 获取 facebook 的好友列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23708063/