2

我尝试从登录的 Facebook 用户那里至少获取 id、用户名和电子邮件。我首先使用权限类型登录:电子邮件。如果成功,请尝试获取 FBSDKGraphRequest,它会返回除电子邮件之外的所有可能值。

我也尝试添加诸如 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);
    }];
}];
4

4 回答 4

2

你的代码应该是这样的

-(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) {
             {

             }
         }
     }];
}
于 2016-02-11T09:36:06.657 回答
0

在登录按钮单击上使用以下代码。

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
         {

         }
     }];
}

可能这对你有帮助。

于 2016-02-11T08:20:28.183 回答
0

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

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

于 2016-03-27T12:33:09.937 回答
0

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

于 2018-10-10T15:58:57.947 回答