Facebook iOS: requestNewPublishPermissions not asking user for new authority - ios

I am having a problem with requestNewPublishPermissions (I'm using SDK version 3.17). The Facebook docs say to log in with just read permission, and then later ask for write permission, so that's what I'm trying to do. I can get the user to log in just fine with code like this:
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile"]
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
{
// Handler for session state changes
// This method will be called EACH time the session state changes,
// also for intermediate states and NOT just when the session open
[self sessionStateChanged:session state:state error:error];
}];
Then when I want to post to the timeline, I ask to expand the permissions using the code below. The app kicks out to Safari with a Facebook web page. But instead of asking for write permission, that web page just says that the app was already given permission. And then when I hit okay, and control returns to the app, I do NOT have publish permission. See "this is where it ends up!" below.
I have tried going to facebook.com and deleting the permissions already given under "Apps". That didn't make any difference. I've also tried doing a publish anyway, but that gives me an error that says I don't have the correct permissions.
Any ideas?
- (void) requestPublishPermission:(void (^)(void)) action
{
// Request publish_actions
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error)
{
if (!error)
{
if ( [FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound )
{
// this is where it ends up!
}
else
{
// Permission granted, publish the story
action();
}
}
else
{
// permission denied, alert user
}
}];
}

You need to add the user you are logging in as as a Developer/Tester. Or you need to submit your app to Facebook for review.

I have the same issue and it is happening only when the user is not using the FB App and everything is happening via the Web. This is what I did to resolve the problem:
typedef void(^facebookPostCompBlock)(FBSession *session, NSError* err, BOOL publishActionPermissions);
- (void) obtainPostPermissionsWithComplition:(facebookPostCompBlock) comp {
if ([[FBSession activeSession] isOpen]) {
if ([[[FBSession activeSession] permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
[FBSession.activeSession requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error)
{
BOOL gotPublishActionPermissions = TRUE;
if ([[[FBSession activeSession] permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
gotPublishActionPermissions = FALSE;
}
if (comp) {
comp(session,error,gotPublishActionPermissions);
}
}];
} else {
if (comp) {
comp([FBSession activeSession],nil,TRUE);
}
}
} else {
BOOL openedSynchronously = [FBSession openActiveSessionWithPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
{
if (!error && status == FBSessionStateOpen) {
[self obtainPostPermissionsWithComplition:comp];
} else {
BOOL gotPublishActionPermissions = TRUE;
if ([[session permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
gotPublishActionPermissions = FALSE;
BLog(#"+ NOT FOUND +");
} else {
if (comp) {
comp(session,error,gotPublishActionPermissions);
}
}
}
}];
}
}
The only concern in the following (need a fail safe) is the that the session will switch from open to close over an over and you'll end up with a loop.

Related

How can we get Facebook Page List in iOS SDK

I have successfully logged user to Facebook using Facebook Graph API and now i need to fetch the user's Facebook page list (page status is already changed to Published).
My code looks like :
(void)checkLoginWithFacebook {
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
// If the session state is not any of the two "open" states when the button is clicked
[self getListOfPages];
}
else
{
// Open a session showing the user the login UI
// You must ALWAYS ask for basic_info permissions when opening a session
// This will bypass the ios6 integration since it does not allow for a session to be opened
// with publish only permissions!
FBSession* sess = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:#"publish_actions",nil]];
[FBSession setActiveSession:sess];
[sess openWithBehavior:(FBSessionLoginBehaviorForcingWebView) completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
{
[[AppDelegate appDel] sessionStateChanged:session state:state error:error];
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed)
{
NSLog(#"session closed");
return ;
}
[self getListOfPages];
}];
}
}
(void)getListOfPages {
[FBRequestConnection startWithGraphPath:#"/me/accounts"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"pages result: %# ",result);
}];
}
Response:
pages result: {
data = ();
}
Please Advice. Thanks.
Have you requested the manage_pages permission from the respective user through the login dialog? The code you posted look ok IMHO, I think you see an empty result because of the missing permission.

openActiveSessionWithPublishPermissions / requestNewPublishPermissions publish_actions not adding permission

Using SDK 3.22.0.
if (FBSession.activeSession.isOpen) {
[FBSession.activeSession
requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
NSLog(#"error = %#", error);
NSLog(#"session open = %d", session.isOpen);
NSLog(#"session.permissions = %#", session.permissions);
NSLog(#"session.accessTokenData.declinedPermissions = %#", session.accessTokenData.declinedPermissions);
}];
}
else {
[FBSession
openActiveSessionWithPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(#"error = %#", error);
NSLog(#"session open = %d", session.isOpen);
NSLog(#"status = %lu", status);
NSLog(#"session.permissions = %#", session.permissions);
NSLog(#"session.accessTokenData.declinedPermissions = %#", session.accessTokenData.declinedPermissions);
}];
}
I'm testing with a user that doesn't have yet publish permissions and never declined it either. On FB Apps Settings on this account, the App Visibility is set to "Friends" and publishing permissions are not even on the settings list as they are for other users/apps.
In both cases of the code, the FB app opens and returns to my app immediately, without asking permissions. Response of declinedPermissions is an array with publish_actions.
My expectation is that FB app will ask the user to approve publishing.
I got reports from multiple users that experienced the same issue - not being able to add publish permissions, but some are able to get the permission.
One thing to add is that I had the same issue before submitting the app for FB approval with users outside of the test group, but when app got approved it started working for those users. Now it seems like the problem persists even when the app is approved, just for random users.
Am I doing anything wrong with the way I'm asking for permissions?
Looks like this worked:
- (BOOL)hasWritePermissions {
if (!FBSession.activeSession.isOpen) return NO;
return [FBSession.activeSession.permissions indexOfObject:#[#"publish_actions"]] != NSNotFound;
}
- (void)requestWritePermissions:(void(^)(BOOL status, NSError *error))callback {
if (self.hasWritePermissions) {
callback(YES, nil);
return;
}
if (FBSession.activeSession.isOpen) {
[FBSession.activeSession
requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
NSLog(#"error = %#", error);
NSLog(#"session open = %d", session.isOpen);
NSLog(#"session.permissions = %#", session.permissions);
NSLog(#"session.accessTokenData.declinedPermissions = %#", session.accessTokenData.declinedPermissions);
if (self.hasWritePermissions) {
callback(YES, nil);
}
else {
callback(NO, error);
}
}];
}
else {
[FBSession
openActiveSessionWithPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(#"error = %#", error);
NSLog(#"session open = %d", session.isOpen);
NSLog(#"status = %u", status);
NSLog(#"session.permissions = %#", session.permissions);
NSLog(#"session.accessTokenData.declinedPermissions = %#", session.accessTokenData.declinedPermissions);
[self requestWritePermissions:callback]; // this time, with an open session
}];
}
}
If there's no session, I run openActiveSessionWithPublishPermissions and then run again requestNewPublishPermissions.
Issue is that openActiveSessionWithPublishPermissions was firing the callback without even going to Facebook app for more permissions (looks like FB bug, will report), but this approach seems to solve it.
Another issue I found is that session.permissions are not always reflecting the permissions on Facebook. The only way I found to ensure I have the latest permissions is to issue an API request:
[FBRequestConnection startWithGraphPath:#"/me/permissions" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
and check the result.data array for granted/declined permissions.
I had a similar issue with this method. I ended up opening the session with a full set of permissions (read & write ) and it solved my problem.
-(void)requestPublishPermissionsWithCompletion:(FBHandlerComp)completion{
if (self.session.isOpen && [self.session.permissions containsObject:#"publish_actions"]){
//we have an open session and all neceassarry pemissions
completion(true,nil);
}else{
//something in missing, to account to all diffrent scenarios (missing persmission, expired tokens, changes in user sessting etc..), we reinisilise the session and request permissions
//for publish permissions we need to ask for the whole set( read& publish)
NSMutableArray *permissions = [NSMutableArray arrayWithArray:self.writePersmissions];
[permissions addObjectsFromArray:self.readPersmissions];
self.session = [[FBSession activeSession]initWithAppID:nil permissions:permissions defaultAudience:FBSessionDefaultAudienceFriends urlSchemeSuffix:nil tokenCacheStrategy:nil];
[self openFacebookSessionWithCompleteion:^(BOOL result, NSError *error) {
if (result) {
completion(true,nil);
}else{
if (LOGGING_IS_ON) DebugLog(#"could not get publish permissions- could not open session %#",error);
completion(false,nil);
}
}];
}
}

Facebook SDK native login error

Note: I am following the native login advice from Native Facebook Login stopped working after SDK update to 3.14.
The error is as follows:
2014-10-13 20:03:27.378 Registration[1916:407643] Error
Domain=com.facebook.sdk Code=9 "Access has not been granted to the
Facebook account. Verify device settings." UserInfo=0x1753c630
{NSLocalizedDescription=Access has not been granted to the Facebook
account. Verify device settings., NSLocalizedFailureReason=Access has
not been granted to the Facebook account. Verify device settings.}
This is the code below:
// RegistrationManager.m
- (void)setupFacebook
{
FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
};
if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
{
// we have a cached token, so open the session
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
completionHandler:completionHandler];
}
else
{
[self clearAllUserInformation];
// create a new facebook session
FBSession *fbSession = [[FBSession alloc] initWithPermissions:#[#"public_profile"]];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:completionHandler];
}
}
- (void)clearAllUserInformation
{
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession renewSystemCredentials:^(ACAccountCredentialRenewResult result, NSError *error) {
NSLog(#"%#", error);
}];
[FBSession setActiveSession:nil];
}
// RegistrationViewController.m
- (IBAction)loginButtonPressed:(id)sender
{
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended)
{
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else
{
FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
[registrationManager sessionStateChanged:session state:status error:error];
};
// create a new facebook session
FBSession *fbSession = [[FBSession alloc] initWithPermissions:#[#"public_profile"]];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:completionHandler];
}
}
When I click on the button, it simply says
2014-10-13 20:03:29.560 Registration[1916:407643] Session closed
2014-10-13 20:03:29.561 Registration[1916:407643] User logs out.
2014-10-13 20:03:29.573 Registration[1916:407643] User logs out.
It means that the user once selected "Didn't allow" in the pop-up that asks for permission. If he has his Facebook account linked in Settings, from now on, every time he'll try to login it'll show this error. It won't fallback to Safari or anything, just show this error. The user needs to got into Settings -> Facebook and set the slider for your app to ON.

Can't get permissions for user_birthday and user_hometown in facebook-ios-sdk

I am not able to get the permissions for user_birthday and user_hometown.
I tried it with this code before but it only asks for the public profile and ignores others.
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile",#"user_birthday",#"user_hometown"]
allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (error) {
NBAppDelegate* appDel = (NBAppDelegate*)`[UIApplication sharedApplication].delegate;
[appDel sessionStateChanged:session state:status error:error];
}
if ([session isOpen]) {
[self loginWithFBToken:session name:sender];
}
}];
Then someone suggested to ask for additional permissions after getting the public profile, so i even tried that to no good.
Here is the code for that
- (void)loadFbDetails
{
NSArray *permissionsNeeded = #[#"user_hometown", #"user_birthday"];
// Request the permissions the user currently has
[FBRequestConnection startWithGraphPath:#"/me/permissions"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
// These are the current permissions the user has:
NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
// We will store here the missing permissions that we will have to request
NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:#[]];
// Check if all the permissions we need are present in the user's current permissions
// If they are not present add them to the permissions to be requested
for (NSString *permission in permissionsNeeded){
if (![currentPermissions objectForKey:permission]){
[requestPermissions addObject:permission];
}
}
// If we have permissions to request
if ([requestPermissions count] > 0){
// Ask for the missing permissions
[FBSession.activeSession
requestNewReadPermissions:requestPermissions
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// Permission granted
NSLog(#"new permissions %#", [FBSession.activeSession permissions]);
// We can request the user information
[self makeRequestForUserData];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
} else {
// Permissions are present
// We can request the user information
[self makeRequestForUserData];
}
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
}
-(void)makeRequestForUserData
{
[FBRequestConnection startWithGraphPath:#"me?fields=birthday,hometown"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Sucess! Include your code to handle the results here
NSLog(#"user events: %#", result);
} else {
NSLog(#"error: %#" , error);
}
}];
}
All it does is recursively go between my ios app and the fb native app, returning only with the public_profile in the permissions array.
Looks like i am missing something?
It should work if you're using an admin user of the app. Once you want to use the extended permissions with other users, you have to get your app reviewed by Facebook first.
See my answer here: facebook extended permission
Facebook does not allow apps to access that information by default. You have to ask permission to Facebook for you to be able to use that information. Add a video and instructions for a Facebook employee review how you're using birthday and hometown intel.

FBSession: an attempt was made reauthorize permissions on an unopened session

I'm following this tutorial
to implement the view preview post on Facebook SDK 3.1, but when I call this method
...
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self publishStory];
}
}];
} else {
// If permissions present, publish the story
[self publishStory];
}
...
returns the following error:
* Terminating app due to uncaught exception 'com.facebook.sdk: InvalidOperationException', reason: 'FBSession: an attempt was made
​​reauthorize permissions on an unopened session'
What is happening can? Thank you!
EDIT:
Ran my friend, thank you very much, but still have a detail ...When'll post the first time he asks to authorize the application I authorize this block of fall
/*
* open a new session with publish permission
*/
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceOnlyMe
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
[self publishStory];
}else{
NSLog(#"error");
//Here I get the error mentioned below
}
}];
}
with the error: error:
domain = com.facebook.sdk, code = 5
The error says that the FBSession is not opened. so you should check if the session is opened before trying to reauthorize.
if ([[FBSession activeSession] isOpen]) {
/*
* if the current session has no publish permission we need to reauthorize
*/
if ([[[FBSession activeSession] permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
[[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObject:#"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error){
[self postPhoto];
}];
}else{
[self publishStory];
}
}else{
/*
* open a new session with publish permission
*/
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceOnlyMe
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
[self publishStory];
}else{
NSLog(#"error");
}
}];
}
Make sure to consistently request for the same permissions which should be publish_actions (mind the plural).
You also need another method in AppDelegate:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(#"In fallback handler");
}];
}
This will handle the url callback if the authentication is done in safari. After that the FBSession will be open.

Resources