不能从iOS 8中获取用户的电子邮件来自Facebook的个人信息

时间:2015-09-09 06:24:59

标签: ios objective-c iphone facebook

在我的应用程序中,我必须实现facebook登录。基于facebook文档,我正在进行集成,但当我提取用户信息时,电子邮件部分对我来说是空的。 有人可以告诉我问题在哪里。 这个代码我正在使用

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

                 [login logOut]; // Only If you don't want to save the session for current app
             }
         }
     }];
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);



        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

             if (!error) {



                 NSLog(@"fetched user:%@  and Email : %@", result,result[@"email"]);
             }
         }];
    }

}

退出这个

fetched user:{
    id = 970029739773026393591;
    name = "jace";
}  and Email : (null)
FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter

3 个答案:

答案 0 :(得分:4)

Facebook已升级了权限,因此请按照此

进行操作

更改此行

 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]

进入

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]

额外reference

答案 1 :(得分:1)

您必须通过参数email来通过Facebook获取user_email:

///Add This line
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
                     [parameters setValue:@"id,name,email" forKey:@"fields"];

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters] <--------- This line

startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                          if (!error)
                          {

                          }

答案 2 :(得分:1)

你必须得到像这样的用户电子邮件

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email", @"user_friends",@"user_birthday"] 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"])
                 {
                     NSLog(@"Login successfull");
                     [self fetchUserInfo];
                 }
             }
         }];
    }

    -(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)
                 {
                  NSLog(@"result : %@",result);
                 }
             }];

        }
    }