Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 468
Premal Khetani Avatar asked Apr 07 '15 08:04

Premal Khetani


People also ask

How do you change permissions on Facebook?

Access PermissionsClick “Privacy Settings.” Click “Edit Settings” next to Apps and Websites to view management options for the apps you currently use or have used.

How do I give an app permission on Facebook?

Tap in the top right of Facebook. Scroll down and tap Settings. Go to the Permissions section and tap Apps and Websites. Go to Apps, Websites and Games and tap Edit.

How do I use OAuth on Facebook?

Under Products in the App Dashboard's left side navigation menu, click Facebook Login, then click Settings. Verify the Valid OAuth redirect URIs in the Client OAuth Settings section. state . A string value created by your app to maintain state between the request and callback.

What is Facebook SDK for IOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.


2 Answers

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.

like image 185
Premal Khetani Avatar answered Nov 04 '22 00:11

Premal Khetani


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";
       }
}
like image 45
barryjones Avatar answered Nov 04 '22 02:11

barryjones