Im Trying to store the users email address in mt parse database but i cant seem to find the right way of getting this done
my code below
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error) {
if (error) {
// Handle it
} else {
PFUser *me = [PFUser currentUser];
me[#"facebookId"] = result.objectID;
me[#"facebookName"] = result.name;
me[#"facebookProfile"] = result.link;
[me saveEventually];
}
}];
}
Thanks in advance
Actually, result is an NSDictionary instance, conforming to FBGraphUser protocol. That mean it still can be treated as dictionary, for example you can send -objectForKey: message to it. Actually FBGraphUser protocol only adds a "shortcuts" for some objects, stored in the dictionary. So, by calling:
NSString *mail = result[#"email"];
You will get the email. This is a handy equivalent for:
NSString *mail = [result objectForKey:#"email"];
For all possible keys for user look Graph Api Reference.
Related
This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 7 years ago.
I am using parse framework to manage users.
I have an option in my app where the users can login from facebook.
I have linked the FacebookAppID and URL scheme in plist file.
According to parse documentation the recommended code here is below
[PFFacebookUtils logInWithPermissions:#[ #"email",#"user_about_me", #"user_relationships", #"user_birthday", #"user_location"] block:^(PFUser *user, NSError *error)
{
if (user)
{
NSLog(#"User authenticated from facebook.");
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"Facebook id fetched.");
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
}
}
else
{
// User not authenticated
}
]];
Now as you can see i have entered proper permissions for email,user_about_me,user_relationships,user_birthday, user_location
But the result dictionary i am getting from facebook only return name and id everytime and for every user.
I also want rest of the information. Any one know why i am not getting all user data?
Its not duplicate
Use this code-
-(void)fectchUserInfo
{
FBRequest *req = [[FBRequest alloc] initWithSession:[FBSession
activeSession] graphPath:#"me?
fields=first_name,last_name,gender,id,email"];
[req startWithCompletionHandler:^(FBRequestConnection *connection, id result,NSError *error) {
if(error) {
NSLog(#"fb error");
[self handleAuthError:error];
return;
}
else{
facebookFirstName = [result objectForKey:#"first_name"];
facebookLastName = [result objectForKey:#"last_name"];
facebookID = [result objectForKey:#"id"];
facebookEmail = [result objectForKey:#"email"];
NSLog(#"fb data:%# %# %# %# ",facebookFirstName,facebookLastName,facebookID,facebookEmail);
facebookPassword = [NSString stringWithFormat:#"%#_%#",facebookID,#"fb"];
NSLog(#"fb password is :%#",facebookPassword);
}
}];
}
I am using:
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
This gives me a JSON string of ALL data (AND I MEAN ALL) from a Facebook Page. It gives me the posts, IDs of those who liked the posts, every comment, every person who shared. I really just need the post itself, which is listed as 'message' in the JSON result. Is there a way I can do this in the API call, or does it have to be done after?
Also,
Is there any way to get it to pull the pictures that are associated with each post? I know how to get photos posted to page, but I just want to view the posts made to the page, and have it also pull up the picture.
You can filter out feed response as below
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:[NSMutableDictionary dictionaryWithObject:#"id, message, link.picture" forKey:#"fields"]]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
As you can check, I've mentioned the parameters which I information I requires to get filter out.
NOTE: You can filter out things according to your requirement.
Please check below link for available filter possibilities on facebook SDK.
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
I hope it will help you, Not sure about the picture you want to pull up but may be 'link.picture' in 'fields' will help you the picture you want to get.
Use
/{page_id}/feed?fields=id,message
With Graph API v2.4 this will be the standard usage that you have to specify each field.
this, it's the variable of NSString "link" that you want:
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *link = userData[#"link"];
NSString *locale = userData[#"locale"];
NSString *timezone = userData[#"timezone"];
NSString *last_name = userData[#"last_name"];
NSString *email = userData[#"email"];
NSString *gender = userData[#"gender"];
NSString *first_name = userData[#"first_name"];
} else if (error) {
//tbd
}
}];
} else if (![FBSDKAccessToken currentAccessToken]) {
//tbd
}
I am trying to get a list of friends for my app, however this is what the console returns:
{
data = (
);
summary = {
"total_count" = 1221;
};
The total count is right but it doesn't show me any of my friends?
I have created two test users on the facebook developer portal and they have both logged in with my app, and they are friends with me on facebook, shouldn't their data be showing?
Here is my code for requesting the permissions and such, this class is called each time the user logs in.
#implementation UploadUser
NSString *userName;
NSString *userId;
NSString *userGender;
//UIImage *userPicture;
NSString *userPicture;
NSMutableURLRequest *urlRequest;
//NSData *imageData;
//NSData *pictureData;
//NSData *tempData;
//Retrieves users name, gender and id
-(void)getUserInfo {
FBSDKGraphRequest *requestUser = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestUser completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
userName = result[#"name"];
userGender = result[#"gender"];
userId = result[#"id"];
if (connection) {
if (!error) {
[self setUserData];
[self uploadUser];
}
}else if (!connection) {
NSLog(#"Get User %#",error);
}
}];
[connection start];
//Get Friends
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (connection) {
NSLog(#"Friends = %#",result);
}else if (!connection) {
NSLog(#"Get Friends Error");
}
}];
}
//Saves user data to class
-(void)setUserData {
DataManager *dm = [DataManager sharedInstance];
[dm setObject:userName forKey:#"userName"];
[dm setObject:userGender forKey:#"userGender"];
[dm setObject:userId forKey:#"userId"];
NSLog(#"%#",userId);
NSLog(#"%#",userName);
NSLog(#"%#",userGender);
}
//Uploads user data online
-(void)uploadUser {
PFUser *user = [PFUser currentUser];
user[#"name"] = userName;
user[#"id"] = userId;
user[#"gender"] = userGender;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"User Data Uploaded :)");
} else {
//Handle Error
NSLog(#"User Upload Error... %#",error);
}
}];
}
Why are isn't my query returning my friends? Is it because my friends are not authorised to use my app? Is my permissions not being asked properly?
Also you can use Graph API Explorer in order to quickly check if your request works:
https://developers.facebook.com/tools/explorer/
If you want to check you friends you should ask your friend also use this link and get token. In this way the facebook system understand that your friend login in Graph API app and you will get the list of friends.
Since v2.0 of the Graph API, you can only get the friends who authorized your App with user_friends too. This is subject of countless Stackoverflow threads already, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Check this one Get Facebook friends
I am using Parse as my database. I want to access the _Users class to see if my Facebook friends are using the app (in the class _User). I have just read this and found out that you can't save friendlist anymore.
The way I did it so far is:
I first get the FB ids of my friends and query it in _User. However, I noticed that the FacebookIDs are saved as authData. How can I query my _User class using FacebookIDs besides creating a new column duplicating facebookIDs as strings?
// Send request to Facebook
[[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, id result, NSError *error) {
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// result will contain an array with your user's friends in the "data" key
NSArray *friendObjects = [result objectForKey:#"data"];
NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
// Create a list of friends' Facebook IDs
for (NSDictionary *friendObject in friendObjects) {
[friendIds addObject:[friendObject objectForKey:#"id"]];
}
self.fbfriendList = [NSMutableArray arrayWithArray:friendObjects];
PFQuery *friendQuery = [ParseUser query];
//PROBLEM is here where the fbId in my _User class is saved as authData
[friendQuery whereKey:#"fbID" containedIn:friendIds];
NSArray *friendUsers = [friendQuery findObjects];
self.fbfriendList = friendObjects;
}
}];
}];
Parse Users' Facebook IDs are private by default, but you can make them accessible from other users.
To accomplish this, you can use the Facebook API to get the user's Facebook ID, assign it to a field on the Parse User. Then you can construct a query on the User collection to get a list of Parse users whose Facebook IDs are in your user's friend list.
When you do your Login with Facebook, you should do it like this:
// When your user logs in, immediately get and store its Facebook ID
[PFFacebookUtils logInWithPermissions:permissionsArray
block:^(PFUser *user, NSError *error) {
if (user) {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Store the current user's Facebook ID on the user
[[PFUser currentUser] setObject:[result objectForKey:#"id"]
forKey:#"fbId"];
[[PFUser currentUser] saveInBackground];
}
}];
}
}];
Then, when you are ready to search for your user's friends, you would issue another request:
// Issue a Facebook Graph API request to get your user's friend list
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// result will contain an array with your user's friends in the "data" key
NSArray *friendObjects = [result objectForKey:#"data"];
NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
// Create a list of friends' Facebook IDs
for (NSDictionary *friendObject in friendObjects) {
[friendIds addObject:[friendObject objectForKey:#"id"]];
}
// Construct a PFUser query that will find friends whose facebook ids
// are contained in the current user's friend list.
PFQuery *friendQuery = [PFUser query];
[friendQuery whereKey:#"fbId" containedIn:friendIds];
NSArray *friendUsers = [friendQuery findObjects];
}
}];
Best regards,
I'm using Facebook SDK to get user's details with permissions:
[NSArray arrayWithObjects:#"user_photos",#"public_profile",#"user_location", #"user_birthday",nil];
I know how to get the city but how can I get user's country?
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *name = userData[#"name"];
NSString *city = userData[#"location"][#"name"];
}
else
{
//...
}
}];
It should be [#"location"][#"country"] as #trick14 outlined. If you can't get a value from it, it can have multiple reasons:
The user you're querying has no location information in his profile
The Access Token you're using has no granted user_location permission (Check with a call to /me/permissions)
There's no country info for the location (quite unlikely)
You have a problem with your permissions string BTW, user_location is contained twice, and offline_access and publish_stream are deprecated long ago.
try this:
NSString *city = userData[#"location"][#"city"];
See here for more info.. :)