how to access facebook friends list using social auth in android?

2.6k views Asked by At

I am using social auth library for facebook integration. I am able to share/post text or images but i want to access my friend list.

Is it possible to access friend list via social auth in android?

Thanks in advance...

3

There are 3 answers

1
Tushar Pandey On

https://developers.facebook.com/tools/

Use Graph Api Search .

https://graph.facebook.com/YOUR_ID?fields=friends , will show you whole list of friends .

https://graph.facebook.com/YOUR_ID?fields=friendlists , will show you only 25 friends .

1
Walter On

I am using socialauth to get a list of friends in Facebook. They have more up to date information on their github page wiki. The code below is my adaptation of the examples in the wiki. I am getting Name, profile image and Facebook id.

in my Fragment:

public class Friends extends SherlockFragment {
 private static SocialAuthAdapter facebookAdapter;

I set up the adapter

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    facebookAdapter = new SocialAuthAdapter(new FacebookListener());

later in onCreateView I authorize the user

facebookAdapter.authorize(getSherlockActivity(), Provider.FACEBOOK);

Now I implement the first Listener (did they authorize)

private final class FacebookListener implements DialogListener 
    {
        public void onComplete(Bundle values) {

            Log.d("SocialAuth","Successful");
            facebookAdapter.getUserProfileAsync(new FacebookProfileDataListener());
            Toast.makeText(getSherlockActivity(),"Connection Successful",Toast.LENGTH_LONG).show();


        }

        @Override
        public void onError(SocialAuthError error) {
            Log.d("Custom-UI" , "Error");
        }

        public void onCancel() {
            Log.d("Custom-UI" , "Cancelled");
        }

        @Override
        public void onBack() {
            // TODO Auto-generated method stub

        }


    }

Now I get the profile

public final class FacebookProfileDataListener implements org.brickred.socialauth.android.SocialAuthListener<Profile>  {

        @Override
        public void onError(SocialAuthError arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onExecute(String arg0, Profile profile) {
            if (profile.getProviderId().equalsIgnoreCase(Provider.FACEBOOK.name())) {
                //store some of the profile information here
            }
            facebookAdapter.getContactListAsync(new FacebookContactDataListener());   
        }

    }

Now the Contact List

  public final class FacebookContactDataListener implements SocialAuthListener<List<Contact>> {

@Override
public void onError(SocialAuthError arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onExecute(String arg0, List<Contact> t) {
    List<Contact> contactList = t;
    JSONArray listOfIds = new JSONArray();

    for (Contact c: contactList) {
        listOfIds.put(c.getId());
    }
    AppDataManager manager = AppDataManager.getInstance(getSherlockActivity());
        manager.setFacebookFriendsIds(listOfIds);
        manager.setFacebookFriends(contactList);

}

}

At this point I have a List that I can work with.

0
Karim On

This is the correct answer: add this to your oauth_consumer.properties file, under the line containing graph.facebook.com.consumer_secret:

graph.facebook.com.custom_permissions = user_friends

or read_friendlists depending on what permissions you truly need.

Check https://developers.facebook.com/docs/facebook-login/permissions/v2.0 for more info about permissions.