如何使用 Facebook iOS SDK 4.x“重新请求”电子邮件权限?

How to “rerequest” email permission using Facebook iOS SDK 4.x?

我正在将 FacebookSDK 4.x 与自定义集成 UI 并使用以下方法登录并获得用户的电子邮件许可。

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error){
            NSLog(@"%@",[error localizedDescription]);            
        }
        else if (result.isCancelled){
            NSLog(@"Cancled");
        }
        else
        {
            if ([result.grantedPermissions containsObject:@"email"])
            { 
                NSLog(@"Granted all permission");
                if ([FBSDKAccessToken currentAccessToken])
                {
                    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
                    {
                        if (!error)
                        {
                            NSLog(@"%@",result);
                        }
                    }];
                }
            }
            else
            {
                NSLog(@"Not granted");
            }
        }
    }];

这很好用,除非用户拒绝访问 "email"。我在 FacebookSDK 文档中看到我可以重新请求一次访问用户的电子邮件。根据 FB 文档:如此 link

中给出
If someone has declined a permission for your app, the login dialog won't let your app re-request the permission unless you pass auth_type=rerequest along with your request.

启用重认证

During your chosen login flow we showed you how to use the Login Dialog and OAuth to authenticate someone and request permissions from them. To re-authenticate, you can use these same steps with additional parameters to force it:

auth_type: this parameter specifies the requested authentication features (as a comma-separated list). Valid options are: https - checks for the presence of a secure Facebook session and asks for re-authentication if it is not present reauthenticate - asks the person to re-authenticate unconditionally

auth_nonce: includes an app generated alphanumeric nonce which can be used to provide replay protection. See how to check an auth_nonce

for more.

Here is an example using the JavaScript SDK that triggers re-authentication using an auth_type of reauthenticate:

FB.login(function(response) { // Original FB.login code }, { auth_type: 'reauthenticate' })

Note that the body of the response contains the auth_type parameter you specified, for example:

access_token=USER_ACCESS_TOKEN&expires=SECONDS_UNTIL_TOKEN_EXPIRES&auth_type=reauthenticate

如何通过 iOS SDK 将 "auth_type=rerequest" 传递到 Facebook 的服务器?有什么特殊的方法吗?

我通过回忆方法解决了问题:

[登录 logInWithReadPermissions:@[@"email"] 处理程序:^(FBSDKLoginManagerLoginResult *result, NSError *error)

我需要在再次请求时传递 auth_type: 'rerequest' 我在这个方法的文档中得到了它 我得到了描述 :

Use this method when asking for read permissions. You should only ask for permissions when they are needed and explain the value to the user. You can inspect the result.declinedPermissions to also provide more information to the user if they decline permissions.

If [FBSDKAccessToken currentAccessToken] is not nil, it will be treated as a reauthorization for that user and will pass the "rerequest" flag to the login dialog.

只是问题是我正在调用 logout FbSDKLoginManager class 的方法。这将清除令牌并将其视为旧权限而不是重新请求的权限。

您实际上不需要传递或调用 auth_type:rerequest。 facebook 已经这样做了。如果您检查 FBSDKLoginManager,代码会为您完成。

-(NSDictionary *)logInParametersWithPermissions:(NSSet *)permissions
{
    if ([FBSDKAccessToken currentAccessToken]) 
       {
             loginParams[@"auth_type"] = @"rerequest";
       }
}