简体   繁体   中英

How to fetch complete user details from Facebook API

How to fetch complete userDetails from the Facebook API

    - (void)viewDidLoad {
        [super viewDidLoad];

        // Uncomment to automatically sign in the user.
        _signInFacebook.delegate = self;
        _signInFacebook.readPermissions =  @[@"email",@"birthday"];

    }

- (void)  loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                error:(NSError *)error{
    NSLog(@"LOGGED IN TO FACEBOOK");
    [self fetchUserInfo];
}


-(void)fetchUserInfo {

    FBSDKAccessToken* token = [FBSDKAccessToken currentAccessToken];
    [[[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"/%@",token.userID] parameters:@{ @"fields":@"id,name,first_name,middle_name,last_name,email,gender,location,picture.type(large),age_range,verified,birthday"}  HTTPMethod:@"GET"]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"fetched user:%@", result);

             if ([result isKindOfClass:[NSDictionary class]])
             {
                 NSLog(@"%@",result);
                 [USERDEFAULTS setObject:result forKey:PROFILE_KEY];
             }

         }
         else
         {
             NSLog(@"Error fetchUserInfo %@",error);
         }
     }];

}

Expecting mobile number to fetch from facebook API

You should use "picture" instead of "photo" to get user profile photo. And you can get userID form the accessToken like:

FBSDKAccessToken* token = [FBSDKAccessToken currentAccessToken];
NSString *ID = token.userID

When log in:
To get birthday, you need permission "user_birthday"
To get location, you need permission "user_location"

You can test everything in Facebook Graph API Explorer before implementing in your app.

To request for permission when logging in, sample code:

FBSDKLoginManager* login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile",@"email",@"user_birthday"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    //your code to handle login result
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM