How to get Facebook access token of the current PFUser - ios

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]

Related

FBSDKProfile currentProfile is nil but FBSDKAccessToken currentAccessToken has value

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.

Missing e-mail when using ParseUI with Facebook login/signup

I'm using the the PFLogInViewController of the ParseUI framework. I have two questions related to emails with regards to Facebook Login within the framework:
1) When signing up via Facebook, I see that the email field of my new user is 'undefined.' How can I obtain the user's email address?
2) If I am able to obtain the email address, does parse's application setting of 'emailVerified' still send a verification email to the user upon Facebook signup? If not, how can this be done?
Thanks.
In order to read the user's email address you'll need to add email to your array of Facebook permissions you're requesting. Look at the Parse tutorial on this https://www.parse.com/tutorials/login-and-signup-views#properties
In your case, you'll want to add #"email" like this:
[logInViewController setFacebookPermissions:[NSArray arrayWithObjects:#"friends_about_me", #"email", nil]];
Then when you are requesting the user information from Facebook, you will be able to access the user's email address:
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Now you can access user's email
NSString *email = result[#"email"];
if (email) {
// Save it to your email field in Parse
}
}
}];
Regarding part 2 of your question, I'm not as familiar with the email validation functionality that Parse supports, but looking at this (admittedly old) response https://www.parse.com/questions/email-verification-emails-going-out-to-facebook-users it appears that Parse will validate email addresses even when the user logs in with Facebook.

PFInstallation and adding a pointer to a PFUser

I am trying to get to grips with Parse and build a simple chat app. For my login and signup code I have this snippet:
PFInstallation *installation = [PFInstallation currentInstallation];
installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
This code connects a PFInstallation to a PFUser so a push can be sent by querying a username.
When the app loads I first check to see if there is already a user logged in:
if ([PFUser currentUser]) {
[self performSegueWithIdentifier:#"showFriends" sender:nil];
}
if a user is already logged in the show friends view controller is loaded. Do I need to set the installation again in this code to match the user? i.e.
if ([PFUser currentUser]) {
PFInstallation *installation = [PFInstallation currentInstallation];
[installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
[self performSegueWithIdentifier:#"showFriends" sender:nil];
}
Or is there no need because the user is already logged in? Am I right in thinking that the installation file is UNIQUE and only created once, matching the device to the push service so nothing really changes in that file unless I want to update the PFUser field I added?
thanks
if a user is already logged in the show friends view controller is loaded. Do I need to set the installation again in this code to match the user?
No. Installations and User classes act independently, but in your case since you set a relation then they can act together as well. Since you already set it in application didFinishLaunchingWithOptions: that device has uniquely identified its installation with the token you've provided (device token) so you don't have to call it again.
A User session is different. If you want the User to be logged in you will have to present the login VC somewhere, since it won't be there the first launch.
Am I right in thinking that the installation file is UNIQUE and only created once, matching the device to the push service so nothing really changes in that file unless I want to update the PFUser field I added? thanks
Yes. That's pretty accurate. Just don't get confused. PFUser currentUser is not the same as PFInstallation currentInstallation anyone can sign on to a device but the app can only be installed once on a device making the installation unique. Not users.

Cant get FBGraphUser email

I use Facebook login with my app. (Facebook iOS SDK version 3.11.1)
I ask for "email" permission:
NSArray *permissions = [NSArray arrayWithObjects: #"basic_info", #"email", nil];
Most of the time, I do get the user's email like that:
NSString *email = [user objectForKey:#"email"];
//user is (NSDictionary<FBGraphUser> *)
Sometimes I just don't. (I can see that the NSDictionary is not including email).
I am using this fix so the app won't terminate when i use email later and its nil:
NSString *email = [user objectForKey:#"email"] ? [user objectForKey:#"email"] : #"NO_EMAIL";
But i need the real mail, so i have to come up with a new solution.
I haven't noticed something special with the problematic users.
Any ideas what can be the problem?
It appears that its a well known problem... For many reasons, not everyone on Facebook have Email address registered.
But as i said, my problem is that i need a real mail.
So the simplest solution is to use the user's Facebook Email : user_name#facebook.com
NSString *email = [user objectForKey:#"email"] ? [user objectForKey:#"email"] : [NSString stringWithFormat:#"%##facebook.com", user.username];
some links ref to that you can't sent HTML emails to the Facebook mail, only plain text!
Here is how i have done it and it always works great for me in every situation:
(I suppose that you have added email permission in your code)
Step 1
Open Facebook Framework folder in Xcode and find the FBGraphUser.h class
As you see, you have there all the properties that you use from Facebook Framework, to take the user details, so add an other property there (copy and paste the code below):
Step 2
#property (retain, nonatomic) id<FBGraphUser> email;
And you are good to go!
You could check for a couple of things:
Make sure you are requesting email scope permission in the login request dialog:
https://www.facebook.com/dialog/oauth?client_id=APP&redirect_uri=REDIRECT&state=UNIQUE&scope=email
Check if the access token you got actually has email permission granted with the Access Token Debugger.
All facebook accounts which are verified through Phone Number instead of email will get NULL for [user objectForKey:#"email"]
Facebook won't return the email address if it's not verified.
https://developers.facebook.com/docs/graph-api/reference/v2.2/user
This person's primary email address listed on their profile. This field will not be returned if no valid email address is available.

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.

Resources