FBSDK findByGraphPath (me) 没有 return 一封电子邮件,即使有权限和显式字段

FBSDK findByGraphPath (me) doesn't return an email, even with permissions and explicit fields

我尝试从登录的 Facebook 用户至少获取 ID、用户名和电子邮件。我首先使用权限类型登录:电子邮件。如果成功,请尝试获取 FBSDKGraphRequest,其中 returns 除了电子邮件之外的所有可能值。

我也尝试添加 public_profile 之类的权限,但无论如何我尝试了,但我没有收到电子邮件。我查看了开发人员仪表板,默认情况下似乎已批准电子邮件。

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
        return;
    } else if (result.isCancelled) {
        return;
    }

    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, gender, first_name, last_name, locale, email"}];

    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
            return;
        }

        NSLog(@"Fetched user: %@", result);
    }];
}];

在登录按钮点击时使用以下代码。

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager setLoginBehavior:FBSDKLoginBehaviorSystemAccount];
[loginManager logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
    if (error)
    {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [loginManager logOut];
    }
    else if (result.isCancelled)
    {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [loginManager logOut];
    }
    else
    {
        if ([result.grantedPermissions containsObject:@"publish_actions"])
        {
            NSTimeInterval addTimeInterval = 60*60*24*365*50;
            NSDate *expireDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
            NSDate *refreshDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];

            FBSDKAccessToken *newAccessToken = [[FBSDKAccessToken alloc] initWithTokenString:[[FBSDKAccessToken currentAccessToken] tokenString] permissions:nil declinedPermissions:nil appID:FACEBOOK_ID userID:[[FBSDKAccessToken currentAccessToken] userID] expirationDate:expireDate refreshDate:refreshDate];
            [FBSDKAccessToken setCurrentAccessToken:newAccessToken];
            [self FacebookUserInfo];
        }
        else
        {
            [loginManager logInWithPublishPermissions:@[@"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
            {
                if (error)
                {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    [loginManager logOut];
                }
                else if (result.isCancelled)
                {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    [loginManager logOut];
                }
                else
                {
                    NSTimeInterval addTimeInterval = 60*60*24*365*50;
                    NSDate *expireDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
                    NSDate *refreshDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];

                    FBSDKAccessToken *newAccessToken = [[FBSDKAccessToken alloc] initWithTokenString:[[FBSDKAccessToken currentAccessToken] tokenString] permissions:nil declinedPermissions:nil appID:FACEBOOK_ID userID:[[FBSDKAccessToken currentAccessToken] userID] expirationDate:expireDate refreshDate:refreshDate];
                    [FBSDKAccessToken setCurrentAccessToken:newAccessToken];
                    [self FacebookUserInfo];
                }
            }];
        }
    }
}];

获得访问令牌后调用以下方法。

- (void)FacebookUserInfo
{
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields":@"name,email,first_name,last_name"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
     {
         if (!error)
         {
             NSDictionary *dict = @{@"id":result[@"id"],
                                          @"name":result[@"first_name"],
                                          @"email":result[@"email"]
                                          };
             [self saveData:dict];
         }
         else
         {

         }
     }];
}

可能这对你有帮助。

你的代码应该是这样的

-(void)loginWithFacebookButtonTouchUpInside
{
    if(![FBSDKAccessToken currentAccessToken])
    {
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
        [login logInWithReadPermissions:@[@"email",@"public_profile"] fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            if (error) {
                // Process error
            }
            else if (result.isCancelled)
            {
                // Handle cancellations
            }
            else
            {
                // If you ask for multiple permissions at once, you
                // should check if specific permissions missing
                [self userLoggedIn];
            }

        }];
    }else{
       //error "Authentication is not done"
    }
}

- (void)userLoggedIn
{
    [FBSDKProfile enableUpdatesOnAccessTokenChange:NO];
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"name, email,first_name,last_name"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id userInfo, NSError *error) {
         if (!error) {
             {

             }
         }
     }];
}

我刚刚遇到了同样的问题,我很幸运在评论中找到了解决方案。它由 CBroe 提供。我认为这应该作为答案发布。

所以...在我的情况下,问题是用户没有确认他的电子邮件。确认后,我收到了来自 Facebook 的电子邮件。所有致谢均致 CBroe

在 Facebook 上确认您的电子邮件。问题将得到解决。