Get Facebook Friends returns empty - ios

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

Related

schedule post on facebook page using facebooksdk

I have written following code to post on facebook page but getting this error message: “(#200) Unpublished posts must be posted to a page as the page itself.“;
NSString *accesstoken = #"EAACEdEose0cBAAe49xNZBS5MqswXkRptXgLDRuZBZBPZCZCVl5rxW6Bt0jthaHXMACNTiUWePg9XkVTpttDNsLyWYBaEkQaUCm5vbSJQh8zGwkegAcQRjRggAJajunmMyg0jVIKZAZBzMjbZCKWUv1Tie1UzF8gJCpIxACIVgqx7SqKdPZBnIUaElk3meB7SYo6AZD";
NSString *pageId = [dic valueForKey:#"id"];
NSDictionary *dicPara=#{#"message": #"Hello",#"access_token":accesstoken,#"scheduled_publish_time": [NSNumber numberWithInt:timestamp],#"published": #"false"};
NSLog(#"%#",dicPara);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/feed",pageId]
parameters:dicPara
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
if (error)
{
NSLog(#"%#",error);
}
else
{
[arrPostId addObject:[result valueForKey:#"id"]];
}
}];
Now I have searched for this error and found that you have to set accesstoken of the page instead of user's token. So how can I change user's access token to page accesstoken?

Parse Facebook Login not returning all fields (it returns only id and name) [duplicate]

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);
}
}];
}

How can I get a user's FB ID in Parse? Can I access AuthData?

How can I get the Facebook ID of a user that "linked" their Facebook account with my Parse app? I see there is an authData column but when I request the class it only returns select columns. How can I do this?
You get facebook id of user using parse own method with facebook api FBRequestConnection
[PFFacebookUtils logInWithPermissions:[NSArray arrayWithObject:#"email"] block:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew) {
NSLog(#"User signed up and logged in through Facebook!");
[FBRequestConnection startWithGraphPath:#"/me"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSString *facebookID = result[#"id"];
NSString *nameF = result[#"first_name"];
NSString *nameL = result[#"last_name"];
NSString *gender = result[#"gender"];
NSString *email = result[#"email"];
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", facebookID]];
NSData *imageData = [NSData dataWithContentsOfURL:pictureURL];
//UIImage *fbImage = [UIImage imageWithData:imageData];
PFFile *imageFile = [PFFile fileWithName:#"Profileimage.png" data:imageData];
PFUser *user = [PFUser currentUser];
user[#"email"] = email;
user[#"User_first_name"] = nameF;
user[#"User_last_name"] = nameL;
user[#"User_gender"] = gender;
user[#"user_image"] = imageFile;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// The currentUser saved successfully.
} else {
// There was an error saving the currentUser.
}
}];
/* handle the result */
}];
} else {
}
}];
In this you can see I get full profile of user ..
Thanks , May be help full for you ..

Facebook Login - Get User Data

I'm building an app on iOS using Parse as a backend.
Doing so I use the Parse Facebook SDK in order first to link Facebook to my Parse User and then to query his data and save them.
The Problem is: sometimes I don't get some of the info like eMail (userData[#"email"] is nil), first_name,name .., or the Profile Pic doesn't download.
Not all the time (quite rare actually), and impossible to reliably replicate the issue so I can solve it. And it's often only one missing field not all.
So I got holes in my Database (not cool when it is the user's email address: my main concern)
Here is how I wrote my methods (i edited most of the if(error) management to make it shorter).
Any ideas ?
Thank you guys !
NSArray *permissionsArray = #[#"public_profile",#"email", #"user_friends"];
[PFFacebookUtils initializeFacebook];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (user.isNew) {
NSLog(#"User with facebook signed up and logged in!");
[self fbData:self];
} else {
NSLog(#"User with facebook logged in!");
[self fbDataAlreadyLog:self];
}
}];
Then I call my "fbData" function in order to fill in the user data into Parse:
-(IBAction)fbData:(id)sender{
[PFFacebookUtils initializeFacebook];
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *userCloud, NSError *error) {
if (!error) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = userCloud;
NSString *facebookID = userData[#"id"];
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", facebookID]];
PFUser *user = [PFUser currentUser];
if (userData[#"name"]) {
user[#"name"] = userData[#"name"];
}
if (userData[#"first_name"]) {
user[#"first_name"] = userData[#"first_name"];
}
if (userData[#"last_name"]) {
user[#"last_name"] = userData[#"last_name"];
}
if (userData[#"id"]) {
user[#"fbID"] = userData[#"id"];
}
if (userData[#"gender"]) {
user[#"gender"] = userData[#"gender"];
}
if (userData[#"email"]) {
user.email = userData[#"email"];
user.username = userData[#"email"];
}
if (userData[#"age_range"]) {
user[#"age_range"] = userData[#"age_range"];
}
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:pictureURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished,NSURL *imageURL)
{
if (image)
{
// do something with image
NSData *imageNSData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:#"profilePic" data:imageNSData];
user[#"image"] = imageFile;
[user saveInBackgroundWithBlock];
//Once the Save Block is done I managed a few more thing before moving on to the next ViewController
}
}];
}];
try like this
NSArray *permissionsArray = #[ #"user_about_me", #"user_relationships", #"user_birthday", #"user_location"];

Getting friends on facebook's scores from parse

So I finally got my app letting users sign into Facebook and parse. Lets call my app a game even though it isn't and lets say that I upload the newest score someone gets every time they play the game. So what I have done is uploaded the "score" to parse under the user. Now, I want the user to be able to see friends that are on Facebook's score. So, I have the user signed in and now how can I check if their friend is using my "game" and then match their email with the one on parse to get there score? As of now, I can get the friends id's of the user that signed in.
BTW. the user has to sign into facebook and make a parse account for my app
I know I am gong to have to use a query to get stuff from parse, I guess my main question is How do I get the facebook users email?
This is how I am getting their id :
- (void)getFacebookInfo {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Set user's information
NSString *facebookId = [result objectForKey:#"id"];
NSString *facebookName = [result objectForKey:#"name"];
if (facebookName && facebookName.length != 0) {
[[PFUser currentUser] setObject:facebookName forKey:#"displayName"];
}
if (facebookId && facebookId.length != 0) {
[[PFUser currentUser] setObject:facebookId forKey:#"facebookId"];
}
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// Get user's friend information
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *data = [result objectForKey:#"data"];
NSMutableArray *facebookIds = [[NSMutableArray alloc] initWithCapacity:data.count];
for (NSDictionary *friendData in data) {
[facebookIds addObject:[friendData objectForKey:#"id"]];
}
[[PFUser currentUser] setObject:facebookIds forKey:#"facebookFriends"];
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// We're in!
NSLog(#"%#", facebookName);
[self dismissViewControllerAnimated:YES completion:NULL];
}];
} else {
[self showErrorAlert];
}
}];
}];
} else {
[self showErrorAlert];
}
}];
}
In order to get the email address of the Facebook user within the completion handler of startForMyFriendsWithCompletionHandler use:
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *data = [result objectForKey:#"data"];
for (NSDictionary *friendData in data) {
//This is each friend's email address
NSString *aFriendsEmail = [friendData objectForKey:#"email"];
}
}
}];

Resources