get facebook friends using restfb

If you are using restfb (Facebook graph API and old REST API client wriiten in Java), and you want to access all friend list after Oauth authentication, you can use following method,



public List<ArrayList> findFacebookFriendsUsingRest(String facebookAccessToken){
  List<ArrayList> myFacebookFriendList= new ArrayList();
  final FacebookClient facebookClient;
  facebookClient = new DefaultFacebookClient(facebookAccessToken);
  User user = facebookClient.fetchObject("me", User.class);
  String userName = user.getFirstName();
  if (userName == null){
  userName = user.getLastName();
  }
  String userEmail = user.getEmail();
  com.restfb.Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
  System.out.println("Count of my friends: " + myFriends.getData().size());
  for(User friend: myFriends.getData()){
  System.out.println("Friends id and name: "+friend.getId()+" , "+friend.getName());
    myFacebookFriendList.add(friend.getName());
  }
  System.out.println("All Friends : "+myFacebookFriendList);
}




Comments

  1. This is only for friend's name, how would we fetch their email ids...

    ReplyDelete
  2. Facebook do not allow (at the moment) to access friends emails.

    Ref: https://developers.facebook.com/docs/reference/api/permissions/

    ReplyDelete
  3. I want to generate a graph of friends in my friedlist, so is it possible to get the friendlist of those friends also.

    ReplyDelete
  4. Thank you very much!

    ReplyDelete

Post a Comment

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Upload csv file at Google Cloud Storage using java

Read Images from a xlsx file using Apache POI