0

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"));

user1871869
  • 3,317
  • 13
  • 56
  • 106
  • You can not get a friend’s photos, unless that friend logs in to your app and grants it the necessary permission first. – CBroe Nov 10 '15 at 09:27
  • @CBroe yes, I have made sure I have a friend that logs into my app and have granted the necessary permissions. These are the permissions that I have granted: `loginButton.setReadPermissions(Arrays.asList("user_friends", "public_profile", "email"));` – user1871869 Nov 10 '15 at 09:33
  • None of those permissions has anything to do with access to a user’s photos. (Hint: The appropriate permission has `photos` in its name.) – CBroe Nov 10 '15 at 09:43
  • Perfect thanks. I added that in but I still get the same issue. Do i need to have Facebook review and approve the permission first before I can actually get my friends' profile pictures? I am trying to get the profile picture of each user that is friend's with the logged in user and has the app downloaded. I read the below answer, but it they don't seem to say where I can get a friend's profile picture. I added in the `user_photos` permission also and still can't get 'Got Photo` to print. @CBroe – user1871869 Nov 11 '15 at 08:46
  • If you only want their current profile picture, then you don’t need `user_photos` permission. (You just said you wanted “friend’s photos” before, which is a different issue.) You can simply request `/me/friends?fields=picture`, that will give you all friend’s profile pictures. Or, if you have their user ids already, you can use `http://graph.facebook.com/{user-id}/picture`, that will redirect to the picture on Facebook’s CDN directly. – CBroe Nov 11 '15 at 09:07
  • @CBroe thanks! I'll try that in a few hours and let you know. – user1871869 Nov 11 '15 at 17:34

1 Answers1

0

I think you are trying to get all the friends of the logged in user, well from Graph API v2 you won't get the list of friends by calling USER_ID/friends. Now this will return users friends who use the app or has authorized himself to the particular facebook app.

Detail information about this can be found here

And regarding about photos of the user read the docs here.


Edit 1

For the profile picture fetch the user id from the graph API and

Add ProfilePictureView in your xml file

    <com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />

Now from Java add the id of the user like this:-

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setProfileId(userId);

Or

Use Picasso to fetch image and add it to an ImageView using this link

https://graph.facebook.com/{facebookId}/picture?type=large
Community
  • 1
  • 1
Aawaz Gyawali
  • 3,244
  • 5
  • 28
  • 48
  • I am trying to get the profile picture of each user that is friend's with the logged in user and has the app downloaded. I read your links, but it they don't seem to say where I can get a friend's profile picture. The first just talks about data about a friend, and the second is about photos of a friend, but not necessarily about getting their profile picture. Any idea on how to do so? I added in the `user_photos` permission also and still can't get 'Got Photo` to print. – user1871869 Nov 11 '15 at 08:45