问题
I have created an app on facebook and achieve some first steps for implementing Facebook SDK.
This is what I'm trying to do: get list of friends from Facebook and to be able to pick some friends from that list and import them to my app.
How an I do that?
回答1:
Are you trying to get the Facebook friendlist of the user logged in into your app? Sounds like you need to perform a facebook graph request to acquire that list, and then withdraw the friends you want.
https://developers.facebook.com/docs/graph-api/reference/user/friendlists/
If you wanted to do it in Android java, her is an example:
AccessToken token = AccessToken.getCurrentAccessToken();
GraphRequest graphRequest = GraphRequest.newMeRequest(token, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
JSONArray jsonArrayFriends = jsonObject.getJSONObject("friendlist").getJSONArray("data");
JSONObject friendlistObject = jsonArrayFriends.getJSONObject(0);
String friendListID = friendlistObject.getString("id");
myNewGraphReq(friendListID);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle param = new Bundle();
param.putString("fields", "friendlist", "members");
graphRequest.setParameters(param);
graphRequest.executeAsync();
Since "member" is an edge in "friendlist" you can do a new request with your friendlist Id to get the members of that particular friend list. https://developers.facebook.com/docs/graph-api/reference/friend-list/members/
private void myNewGraphReq(String friendlistId) {
final String graphPath = "/"+friendlistId+"/members/";
AccessToken token = AccessToken.getCurrentAccessToken();
GraphRequest request = new GraphRequest(token, graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
JSONObject object = graphResponse.getJSONObject();
try {
JSONArray arrayOfUsersInFriendList= object.getJSONArray("data");
/* Do something with the user list */
/* ex: get first user in list, "name" */
JSONObject user = arrayOfUsersInFriendList.getJSONObject(0);
String usersName = user.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle param = new Bundle();
param.putString("fields", "name");
request.setParameters(param);
request.executeAsync();
}
In the documentation for Facebook Graph request you can see what you can do with the User objects. I don't have enough rep to post another link unfortunately.
Keep in mind that a user must have signed in with facebook to get the access token needed to do these operations.
Well, hope it was something remotely like this you were looking for.
Edit: this method does not work anymore, the first link about friendlists shows that it is deprecated since 4 April 2018.
来源:https://stackoverflow.com/questions/33656046/how-to-get-friend-list-from-facebook-using-android-facebook-sdk