How to “rerequest” email permission using Facebook

2019-01-19 11:09发布

问题:

I am integrating FacebookSDK 4.x integration with custom UI and using following method to log-in and also getting email permission from user.

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");
            }
        }
    }];

This works great unless the user denies access to "email". I see in the FacebookSDK docs that I can re-request access to the user's email one time. According to the FB docs: as given in this 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.

Enabling Re-authentication

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

How do I pass "auth_type=rerequest" to Facebook's servers via the iOS SDK? Is there a special method for that?

回答1:

I resolved issue by recalling the method :

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

I need to pass auth_type: 'rerequest' while requesting again and i get it in the documentation of this method i get the description :

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.

just the problem was i was calling logout Method of FbSDKLoginManager class. This will clear the token and it will takes it as the old permission not re-requested permission.



回答2:

You actually don't need to pass or call auth_type:rerequest. This is already done by facebook. If you check FBSDKLoginManager, the code does it for you.

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