14

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Premal Khetani
  • 3,175
  • 1
  • 26
  • 58

2 Answers2

15

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.

Premal Khetani
  • 3,175
  • 1
  • 26
  • 58
  • I'm having the same issue, but how did you pass auth_type: 'rerequest'? – joels May 13 '15 at 20:07
  • 3
    Actually i am not passing 're-request' to facebook explicitly but when ever you are logged in and call [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) method again it will take as 're-request'. so just recall this method after some time that's it yes but you must not call logout method before calling this method AGAIN. hope it will help you :) – Premal Khetani May 14 '15 at 05:11
  • so how to do get the users email when you want to use it ? – ArdenDev Jul 30 '16 at 17:44
  • @iPR +1 for not calling logout(); – WaaleedKhan Aug 17 '16 at 08:13
  • I am not able to relogin. Please help me – Ekta Padaliya Mar 17 '18 at 06:55
  • @EktaPadaliya what exactly issue you are facing ? – Premal Khetani Mar 17 '18 at 11:28
  • 1
    You said after some time, can you please tell me how much time it expects? For my case when I call logInWithReadPermissions this after declining some permissions with after proper alert why I need permissions then it takes me to the login screen instead of permission dialog so can tell why this is happing in my case? – python Apr 09 '18 at 16:06
  • @python you can call the next method after 0.01 sec delay as i am doing. – Premal Khetani Apr 14 '18 at 09:46
  • This does not work for me. Using FBSDKLoginKit(4.35.0) – Mocha Jun 18 '19 at 17:40
3

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";
       }
}
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
barryjones
  • 2,149
  • 1
  • 17
  • 21