Facebook iOS SDK: authorization with permissions requires 2 attempts

I have an application that still uses the legacy Facebook class to connect to Facebook. If I allow without additional permissions, everything works fine. But if I really turn on permissions, the first round for authorization always fails (although it gets a valid token!). Am I missing a step?

Here is the code to activate Facebook authorization for the application

- (IBAction) doConnect:(id)sender { NSArray* permissions = [NSArray arrayWithObjects: @"email",@"publish_actions",nil]; [self.facebook authorize:permissions]; } 

Here, the code that is called after the user has granted permissions and control is returned to my application. The URL always contains a pretty marker, even the first time.

 // handle the incoming url from app switching - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [self.facebook handleOpenURL:url]; } 

And here is the FBSessionDelegate method, which is called after a successful connection. Despite the token contained in the above URL, it left the first time we got here. But if I call the doConnect method above, the token will be present when we come here.

 // FBSessionDelegate - (void)fbDidLogin { if( [self.facebook accessToken] == nil ) { NSLog(@"Had an access token above, but not now!"); // If I reinvade the doConnect: method again, it will work!!! } // ... 

}

Looking deeply into the sdk code in FBSession.m, it seems that the requested permissions were not associated with the new token for the first time, causing the session to ignore the new token. First time, cachedPermissions is always an empty list.

  // get the cached permissions, and do a subset check NSArray *cachedPermissions = [tokenInfo objectForKey:FBTokenInformationPermissionsKey]; BOOL isSubset = [FBSession areRequiredPermissions:permissions aSubsetOfPermissions:cachedPermissions]; 
+4
source share
3 answers

You request two types of permissions: read type (email) and write type (publish_actions).

You must use the latest Facebook SDK, v3.1.1, and you will have to separate the read and write separately - especially if you want to support iOS6. At first you can request read only. See the Note at https://developers.facebook.com/docs/tutorial/iossdk/upgrading-from-3.0-to-3.1/ and the read and write request section separately.

+3
source

For Facebook SDK 3.1, use [FBSession activeSession]

 reauthorizeWithPublishPermissions: defaultAudience:completionHandler: 

For Facebook SDK 3.2 use [FBSession activeSession]

 requestNewPublishPermissions: defaultAudience:completionHandler: 
0
source

we can allow the user in one request. This does not require two attempts. To achieve this, we need to do the following: "We must first request permission to publish." Call the method below

 let fbLoginMngr = FBSDKLoginManager(); fbLoginMngr.logOut() fbLoginMngr.logInWithPublishPermissions 

he will ask for the first profile information, then request permission to publish. As soon as we receive the callback, we will request the Graph api to retrieve the profile data as shown below.

 let fbRequest = FBSDKGraphRequest(graphPath:"me", parameters:self.FB_REQ_PARAMS); fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in if error == nil { debugPrint(result) } else { handleError(error) } } 
0
source

Source: https://habr.com/ru/post/1447534/


All Articles