Reputation: 1919
I saw lots of questions like this in Stack Overflow but they didn't solve my problem. I referred this, this and also Documentation links like this and this
I used LoginButton
to login in my App.
I am able to get user's (which is logged in) Name, Email, etc,using following code (it works fine):
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
email = object.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("EMAIL",email);
Log.e("GraphResponse", "-------------" + response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,link,gender,birthday,email");
request.setParameters(parameters);
request.executeAsync();
}
with permissions:
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, callback);
I got JSON in LogCat. But now I want to get Friends list, So I wrote code by seeing Documentation and changed my code slightly as follows:
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
// I saw following code in Documentation
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{friendlist-id}", /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
// handle the result
Log.d("RESPONSE KBT",response.toString());
}
}
).executeAsync();
}
with permissions:
loginButton.setReadPermissions(Arrays.asList("email", "user_friends","read_custom_friendlists"));
loginButton.registerCallback(callbackManager, callback);
I get this in my LogCat:
RESPONSE KBT﹕ {Response: responseCode: 404, graphObject: null, error: {HttpStatus: 404, errorCode: 803, errorType: OAuthException, errorMessage: (#803) Some of the aliases you requested do not exist: {friendlist-id}}}
Upvotes: 3
Views: 1318
Reputation: 5363
Try this:
Permission:
setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
This variable for process your friends:
public static List<TaggableFriends> friendListFacebook = new ArrayList<TaggableFriends>();
This method for get friends (call it in onSuccess()
method in your login class):
public void getFriends()
{
if(AccessToken.getCurrentAccessToken() != null)
{
GraphRequest graphRequest = GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
"me/taggable_friends",
new GraphRequest.Callback()
{
@Override
public void onCompleted(GraphResponse graphResponse)
{
if(graphResponse != null)
{
JSONObject jsonObject = graphResponse.getJSONObject();
String taggableFriendsJson = jsonObject.toString();
Gson gson = new Gson();
TaggableFriendsWrapper taggableFriendsWrapper= gson.fromJson(taggableFriendsJson, TaggableFriendsWrapper.class);
ArrayList<TaggableFriends> invitableFriends = new ArrayList<TaggableFriends>();
invitableFriends = taggableFriendsWrapper.getData();
int i;
for(i = 0; i < invitableFriends.size(); i++)
{
try
{
friendListFacebook.add(invitableFriends.get(i));
}
catch(Exception e){}
}
}else {
}
}
}
);
Bundle parameters = new Bundle();
parameters.putInt("limit", 5000); //5000 is maximum number of friends you can have on Facebook
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
}
Add this class:
TaggableFriendsWrapper:
public class TaggableFriendsWrapper {
private ArrayList<TaggableFriends> data;
private Paging paging;
public ArrayList<TaggableFriends> getData() {
return data;
}
public void setData(ArrayList<TaggableFriends> data) {
this.data = data;
}
public Paging getPaging() {
return paging;
}
public void setPaging(Paging paging) {
this.paging = paging;
}
public class Paging {
private Cursors cursors;
public Cursors getCursors() {
return cursors;
}
public void setCursors(Cursors cursors) {
this.cursors = cursors;
}
}
public class Cursors {
private String after;
private String before;
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
}
}
TaggableFriends:
public class TaggableFriends {
private String id;
private String name;
private Picture picture;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
public class Picture {
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
public class Data {
private String url;
private boolean is_sillhouette;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isIs_sillhouette() {
return is_sillhouette;
}
public void setIs_sillhouette(boolean is_sillhouette) {
this.is_sillhouette = is_sillhouette;
}
}
}
Upvotes: 4
Reputation: 74014
The correct API endpoint to get the friends of the authorized user is /me/friends
and you need to authorize with the user_friends
permission. Keep in mind that you will only get friends who authorized your App with user_friends
too.
You can ONLY get access to ALL friends for tagging (with /me/taggable_friends
) or inviting friends to a game with Canvas implementation (with /me/invitable_friends
).
More information: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Upvotes: 2
Reputation: 65
Try doing without Brackets {}
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friendlist-id", /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
null, ...... );
Upvotes: 0