I am trying to get my Facebook Friend's photos via the Graph API. This was my attempt:
public void getFacebookData(final AccessToken accessToken){
//newMeRequest = My own data
//myFriendsRequest = my mutual friends who have the app downloaded
//Basically make 2 requests to one's Facebook info and return the names, links, id, and picture of the individual
AccessToken accesstoken = AccessToken.getCurrentAccessToken();
JSONArray friends = new JSONArray();
GraphRequestBatch batch = new GraphRequestBatch(
GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject jsonObject,
GraphResponse response) {
// Application code for user
}
}),
GraphRequest.newMyFriendsRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray jsonArray,
GraphResponse response) {
// Application code for users friends
// Insert into our local DB
Toast.makeText(login.this, "" + response.toString(), Toast.LENGTH_SHORT).show();
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject row = jsonArray.getJSONObject(i);
System.out.println("Got id");
datasource.createFriend(row.getString("name"), row.getString("id"));
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + row.getString("id") + "/picture",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
System.out.println("Got photo");
}
}
);
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
System.out.println("FriendsDB: " + jsonArray);
}
}
)
);
batch.addCallback(new GraphRequestBatch.Callback() {
@Override
public void onBatchCompleted(GraphRequestBatch graphRequests) {
// Application code for when the batch finishes
Log.d(TAG, graphRequests.toString());
}
});
batch.executeAsync();
However, the issue is that I can never seem to have the "Got Photo"
message to print. I am confused because I looked at the GraphRequest constructor and found that this method would work best for me. However, the method never completes and I'm really not sure why it would fail. Any help would be appreciated. thanks!
EDIT: I have made sure I have freinds that are authenticated on my app and have the following permissions:
loginButton.setReadPermissions(Arrays.asList("user_friends", "public_profile", "email"));