1

我正在尝试从 Facebook API 获取信息。参数分组在几个目录中。要获取数据,我使用以下 Facebook SDK 中的代码:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"me"
                                  parameters:@{ @"fields" : @"name, birthday"}
                                  HTTPMethod:@"GET"];[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {

];

数据“名称”和“生日”是“字段”的目录名称。但是我想从其他目录(边、参数)中获取更多数据,例如名字、姓氏、电子邮件、id、关于等。我如何编写代码来获取所有数据?

4

2 回答 2

1

把这段代码

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
             NSLog(@"error is :%@",error);
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
             NSLog(@"error is :%@",error);
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 [self fetchUserInfo];
             }
         }
    }];

您可以通过以下方式获取 facebook 用户信息

-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {

                 NSString *photostring=[[[result valueForKey:@"picture"] objectForKey:@"data"] valueForKey:@"url"];
                 photostring = [photostring stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

                 NSLog(@"all data here is:%@",result);
                 NSLog(@"username is :%@",[result valueForKey:@"name"]);
                 NSLog(@"PhotoUrl is :%@",photostring);
                 NSLog(@"mail id is :%@",[result valueForKey:@"email"]);
             }
         }];
    }
}

祝你的项目好运

于 2015-09-21T07:03:30.507 回答
0

首先,您必须将此代码放在登录过程开始的位置,如下所示

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {

    if (error)
    {

     // There is an error here.

    }
    else
    {
        if(result.token)   // This means if There is current access token.
        {    
            // Token created successfully and you are ready to get profile info
            [self getFacebookProfileInfo];
        }        
    }
}];

如果登录成功,则执行此方法以获取用户的公开资料

-(void)getFacebookProfileInfos { 

FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];

FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

      if(result)
      {            
        if ([result objectForKey:@"email"]) {

          NSLog(@"Email: %@",[result objectForKey:@"email"]);

        }
        if ([result objectForKey:@"first_name"]) {

          NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);

        }
         if ([result objectForKey:@"dob"]) {

          NSLog(@"Date of birth : %@",[result objectForKey:@"dob"]);

        }
        if ([result objectForKey:@"id"]) {

          NSLog(@"User id : %@",[result objectForKey:@"id"]);

        }

      }

 }];

[connection start];
}

您也可以点击此链接获取 facbook 用户信息

或者您可以获取以下 Facebook 用户信息

if (FBSession.activeSession.isOpen) {

    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             NSString *firstName = user.first_name;
             NSString *lastName = user.last_name;
              NSString *bateOfBirth = user.bate_Of_Birth;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         }
     }];
}
于 2015-09-20T11:03:44.140 回答