FBSDKProfile currentProfile is nil but FBSDKAccessToken currentAccessToken has value - ios

Just like what the title says, is this a bug or is it really possible? I use the [FBSDKAccessToken currentAccessToken].tokenString to check whether an existing Facebook session is existing, then afterwards I'll use the [FBSDKProfile currentProfile].userID to get the session's userId.
However sometimes I encounter that [FBSDKAccessToken currentAccessToken].tokenString has a value but [FBSDKProfile currentProfile] is nil.
I use this for the auto-login feature of my application.
Thanks for your response!
So I have this code snippet:
if ([FBSDKAccessToken currentAccessToken].tokenString){
//proceed to auto login since Facebook is still logged in
NSLog(#"Facebook user id: %#",[FBSDKProfile currentProfile].userID)
}
The return of that log is nil.

if ([FBSDKAccessToken currentAccessToken].tokenString) {
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
NSLog(#"Facebook user id: %#",[FBSDKProfile currentProfile].userID);
}
you need to call [FBSDKProfile enableUpdatesOnAccessTokenChange:YES] so that it automatically observes changes to the [FBSDKAccessToken currentAccessToken]

You may need to load the current profile, use this following snippet, after the callback runs you will get the profile data unless you don't have current token.

Related

Unable to login via Facebook (Parse): error code 251

I have an app in the App Store which is using Parse and automatically creates anonymous users (I set [PFUser enableAutomaticUser]). So now I have several thousands of users in a Parse users table.
Now I'm trying to implement full profiles and convert anonymous users to non-anonymous by calling:
[PFFacebookUtils linkUser:currentUser permissions:#[#"public_profile", #"email", #"user_friends"] block:^(BOOL succeeded, NSError *error) {
if (succeeded) {
//some logic
} else {
NSLog(#"Error: %#", error);
}
}];
BTW I've also tried:
[PFFacebookUtils logInWithPermissions:#[#"public_profile", #"email", #"user_friends"] block:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew) {
NSLog(#"User signed up through Facebook!");
} else {
NSLog(#"User logged in through Facebook!");
}
}];
But in both cases I get an error: 251 - The supplied Facebook session token is expired or invalid.
What I've already tried:
Logout anonymous user
Refresh session
Close and re-auth session
Nothing here works for me. Does anyone know what to do in this case? How do I correctly convert anonymous user to non-anonymous?
I'm pretty sure the problem is your Facebook App configuration.
Please go to your Facebook App settings (Advanced section) https://developers.facebook.com/apps/YOUR_FB_APP_ID/settings/advanced/
You should have enabled the option "Native or desktop app?" When you do so, you'll have other option with the message "Is your App Secret embedded?" The error 251 on iOS only comes up when you enable this second option because your app secret is not embedded so the token is invalid.
Please go to your settings and make sure the option "Is your App Secret embedded?" is NOT enabled.
I hope it helps.
verify your fb dev account at "Approved Items" > "Login Permissions" must be the same as you requesting in your permissions array in your code. In my case I am using the new API 2.0.
Hope you help it.
Regards from Cancun
The problem for me was that i copied the App Secret when it was in dots, click on the show button in your Developer Settings and copy the string in there on to Parse.
Hope it helps.

Forget previously logged in facebook user?

When I promt the user to log in with their facebook account I do it like this:
FBSession.openActiveSessionWithReadPermissions(["email"], allowLoginUI: true) { (session, state, error) -> Void in
if error == nil && state == .Open {
FBRequest.requestForMe().startWithCompletionHandler({ (connection, data_, error) in
println("Facebook data: ")
if error == nil {
let facebookInfo = data_ as NSDictionary
}
})
}
else {
self.SuperVC!.presentAlertView("Error", message: "Could not connect with facebook.", decline: "Ok", others: [])
}
Now, when its the first time logging in the user can enter their email and password... but the next time its not possible to do so, it remembers the previous user even if I clear token info like this:
FBSession.activeSession().closeAndClearTokenInformation()
I clear the token before promting to login, for testing purposes.
It only forgets if I reset the entire app, by deleting it or in the simulator 'Reset contents and settings'
Lets say that another user wants to log in on the same device, it is not possible.
How do I make it forget the previous user?
For me, none of the solutions above worked.
I was able to logout, but no way to change the user on the next login screen.
I found a way to change the user after logout from FB by initializing the FBSDKLoginManager as follow :
faceBookLoginManager = [[FBSDKLoginManager alloc] init];
faceBookLoginManager.loginBehavior = FBSDKLoginBehaviorWeb;
When using the above code, after the logOut was done using [faceBookLoginManager logOut]; FB login popup asks again for user name/password so another user can be used.
Facebook SDK takes your facebook login access from Safari. App forgets who the previous user was, if you just sign out of facebook in Safari.
I solved my issue by using another method for promting the user credentials.
I used
FBSession.activeSession().openWithBehavior(FBSessionLoginBehavior.ForcingWebView, completionHandler:{
// Stuff...
}
With the .ForcingWebView it always starts over and promting the users ceredntials.
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession setActiveSession:nil];

How to get Facebook access token of the current PFUser

I need to get the Facebook access token of the current user who is logged in using Facebook so that i can send it to a web service to be able to call Facebook Graph APIs
So How do I get the auth data from the [PFUser currentUser]? as I've tried the following but it returns nil:
PFObject *authData = [[PFUser currentUser] objectForKey:#"authData"];
I got the access token from PFFacebookUtils as the following
NSString *accessToken = [[[PFFacebookUtils session] accessTokenData] accessToken];
I know it's an old question, but I had the same problem and PFFacebookUtils don't have a session property anymore, but this works:
[FBSDKAccessToken currentAccessToken]

iOS Facebook SDK isSessionValid returning null

With the Facebook iOS SDK code below, the log is always "(null)"
facebook = [[Facebook alloc] initWithAppId:#"xxxxxxxxx" andDelegate:self];
NSLog(#"%#", [facebook isSessionValid]);
Any ideas what could be going on?
%# is your problem. BOOL'ean values are not objects, so %# is not their string literal. Strangely enough, BOOL's are signed char but can't be NSlog'd as such. Here's how you would do it:
NSLog([facebook isSessionValid] ? #"Yes" : #"No");
To check whether your facebook session is valid or not, type
NSLog(#"facebook session status: %d", [facebook isSessionValid]);
It will print the YES/TRUE and NO/FALSE value into 1 and 0 respectively.
Creating the Facebook object doesn't create a Facebook session. Once you have the Facebook object, you can call the -(void)authorize:(NSArray *)permissions method, which will prompt the user to login, either through the Facebook app, Safari, or a popup (whichever is available in that order). Once the user successfully logs in, the session will have been created and the [facebook isSessionValid] call should return YES.

Can't get user info from Facebook with OAuth 2.0

As soon as the user is logged in, I retrieve the user info using this code:
[_facebook requestWithGraphPath:#"me" andDelegate:self];
It worked for the first few times, but it eventually returned an error. I thought it was a session thing since I have "offline_access" on my permissions. I logged out from Facebook and compiled again from a clean build and it still returns this error:
Error Domain=facebookErrDomain Code=10000 UserInfo=0x54a2930 "Operation could not be completed. (facebookErrDomain error 10000.)"
I'm assuming my access token is valid since I can do posting. Am I right to assume that?
I also noticed that whenever I log in, Facebook doesn't redirect me to ask permission if I allow my app to access my user info.
Can anyone tell me what's wrong?
Answer on authorize command seems to be different. Now, there's no expiration date as it was before. I tried with forcing isSessionValid to return TRUE, and it works.
isSessionValid checks accessToken AND expirationDate. As there's no expiration date, the function return FALSE.
Don't know if it's a temporary modification from facebook, or definitive one. In this case, SDK must be fixed, I presume. And a new call must retrieve expirationDate.
Here's a temporary fix I've used. You can find this method line 46 in FBLoginDialog.m
- (void) dialogDidSucceed:(NSURL*)url {
NSString *q = [url absoluteString];
NSString *token = [self getStringFromUrl:q needle:#"access_token="];
NSDate *expirationDate = [NSDate distantFuture];
if ((token == (NSString *) [NSNull null]) || (token.length ==0)) {
[self dialogDidCancel:url];
[self dismissWithSuccess:NO animated:YES];
} else {
if ([_loginDelegate respondsToSelector:#selector(fbDialogLogin:expirationDate:)]) {
[_loginDelegate fbDialogLogin:token expirationDate:expirationDate];
}
[self dismissWithSuccess:YES animated:YES];
}
}
Does everyone with this issue request the offline_access permission? It's the only reason I can see an expiration date not being sent. Maybe try not requestion the offline_access permission.
I've had same problem. I checked my privacy settings,permissions,other method etc..., but I couldn't solve it.
Many sites said "check permission for accessing to offline_access/read_stream/publish_stream",I think these are important too,but I couldn't fix it.
Then I found following solution finally, it works fine.
You should change "safariAuth:YES" to "safariAuth:NO" on "Facebook.m" file.
- (void)authorize:(NSArray *)permissions
delegate:(id<FBSessionDelegate>)delegate {
[_permissions release];
_permissions = [permissions retain];
_sessionDelegate = delegate;
//[self authorizeWithFBAppAuth:YES safariAuth:YES];
//You can get auth_token in iOS App!
[self authorizeWithFBAppAuth:YES safariAuth:NO];
}
Then you can use following method to get your own info if you are logged in.
[facebook requestWithGraphPath:#"me" andDelegate:self];
if you are not logged in, You should login by following method first.
permissions = [[NSArray arrayWithObjects:
#"email", #"read_stream", #"user_birthday",
#"user_about_me", #"publish_stream", #"offline_access", nil] retain];
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
Following post saved me.Thank you so much!
How to retrieve Facebook response using Facebook iOS SDK
Good Luck & Happy-coding!
Thank you.
I've had this problem, mainly because my Facebook account has is maxed on privacy.
It's most likely the permissions that you passed in during login. BY default it will only give access to public info. Remember there is less and less info that is public in Facebook.
Try changing your permission to something like "read_stream",nil
Then instead of [_facebook requestWithGraphPath:#"me" andDelegate:self];
Try using [_facebook requestWithGraphPath:#"me/home" andDelegate:self];
This should return your news feed.
Worked for me 5 mins ago.
Not much to add here, doesn't work here too. I use the iOS SDK to post to the stream and it worked until yesterday. Today my client called me and complained. Ugh.
I have requested the offline access permission and of course the "publish_stream" permission...

Resources