Skip to content Skip to sidebar Skip to footer

How To Fetch All Photos From Facebook In Android

I am developing an application in which I am using Facebook SDK for different purposes. Currently I have implemented Login through Facebook. Now my next task is to fetch all the ph

Solution 1:

Your code to request your photos is just fine according to the documentation of Facebook SDK however I think that you forgot to request permission from the user who's logged in (you in this case).

Copied from Facebook SDK documentation: A user access token with user_photos permission is required to see all photos that person is tagged in or has uploaded.

The following link gives you a small tutorial on how to add access tokens to the login button: https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3

In your case it would be (code snippet is for activities only, for fragments: click the url above and follow the tutorial there. Pretty straight forward):

...
LoginButton authButton = (LoginButton) findViewById(R.id.ID_OF_YOUR_LOGIN_BUTTON);
authButton.setReadPermissions(Arrays.asList("user_photos"));
...

In your overriding onCreate method.


Method two:

...
Sessionsession= Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
    session.openForRead(newSession.OpenRequest(this)
        .setPermissions(Arrays.asList("user_photos"))
        .setCallback(YOUR_CALLBACK_CLASS_VARIABLE));
} else {
    // Open an active session. Basically happens when someone is already logged in.
}
...

Before making the call to request all the user photos, you can simply open a new request when the user hasn't logged in yet. This can happen in View.OnClickListener class of a Button as it seems that you're not LoginButton of Facebook.

YOUR_CALLBACK_CLASS_VARIABLE is your class implementing Session.StatusCallback. I'm quite certain you know what it is and what it does.


Method three:

...
    Session.NewPermissionsRequestnewPermissionsRequest=newSession
      .NewPermissionsRequest(this, Arrays.asList("user_photos"));
    session.requestNewReadPermissions(newPermissionsRequest);
...

When the user is already logged in (so after the user is logged in) and you wish to request additional permissions from the user to access his photos or whatsoever. This is by the way all written in the docs of Facebook SDK for Android.

For more information: https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3

Solution 2:

If the request returns an empty result, check whether the used Access Token contains the user_photos permission:

You can inspect the Acces Token via

Request:

GET /debug_token?
 input_token={input-token}&
 access_token={access-token}

Post a Comment for "How To Fetch All Photos From Facebook In Android"