You're looking at our questions archive. This question was last updated about 1 year ago.

If you're looking for help, head over to the Parse Developers Google Group or check out the parse.com tag on Stack Overflow.

Help Google Group Stack Overflow

How can I find Parse Users that are Facebook friends with the current user?

63 votes 1 answer 102k views about 1 year ago

1 Answer

Parse Users' Facebook ids are private (and thus cannot be used in a query) by default, and it is up to you as a developer to decide whether this data should be visible to other users of your application.

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.

In iOS, your code to make your users query-able by Facebook ID would look 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];

    // findObjects will return a list of PFUsers that are friends
    // with the current user
    NSArray *friendUsers = [friendQuery findObjects];
  }
}];

For Android, your code to make your users query-able by Facebook ID would look like this:

ParseFacebookUtils.logIn(this, new LogInCallback() {
  @Override
  public void done(ParseUser user, ParseException error) {
    // When your user logs in, immediately get and store its Facebook ID
    if (user != null) {
      getFacebookIdInBackground();
    }
  }
});

private static void getFacebookIdInBackground() {
  Request.executeMeRequestAsync(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {
    @Override
    public void onCompleted(GraphUser user, Response response) {
      if (user != null) {
        ParseUser.getCurrentUser().put("fbId", user.getId());
        ParseUser.getCurrentUser().saveInBackground();
      }
    }
  });
}

Then, when you are ready to search for your user's friends, you would issue another request:

Request.executeMyFriendsRequestAsync(ParseFacebookUtils.getSession(), new Request.GraphUserListCallback() {

  @Override
  public void onCompleted(List<GraphUser> users, Response response) {
    if (users != null) {
      List<String> friendsList = new ArrayList<String>();
      for (GraphUser user : users) {
        friendsList.add(user.getId());
      }

      // Construct a ParseUser query that will find friends whose
      // facebook IDs are contained in the current user's friend list.
      ParseQuery friendQuery = ParseQuery.getUserQuery();
      friendQuery.whereContainedIn("fbId", friendsList);

      // findObjects will return a list of ParseUsers that are friends with
      // the current user
      List<ParseObject> friendUsers = friendQuery.find();
    }
  }
});

The Facebook Graph API request will get you a list of ids which correspond to your Facebook friend's Facebook IDs. It will then query for PFUsers in your Parse app which have a matching Facebook ID. If you don't have any common Facebook friends in your PFUser table, this query will return an empty set. You can try setting it up with some sample data based on your existing Facebook friends.

- Héctor Ramos over 2 years ago

Updated my Android example to use the new Facebook 3.0 SDK as documented in the Facebook API Reference: https://developers.facebook.com/docs/reference/android/3.0/Request

- Héctor Ramos over 2 years ago

Thanks, Hector, that's perfect.

- Thom over 2 years ago

Hector for presidente!

- Duncan Campbell over 2 years ago