Salman Khan
Salman Khan

Reputation: 2832

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 photos of the logged in user and display all of them in a grid. I am following the official facebook developer website as a reference for this task. Following is the code that I am using to fetch the user photos.

    new Request(
            Session.getActiveSession(),
            "/me/photos",
            getRequestParameters(),
            HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    /* handle the result */
                    Log.d("Response ","Response " + response);
                    mpBar.setVisibility(View.GONE);
                }
            }
        ).executeAsync();

private Bundle getRequestParameters() 
    {
        String accessToken = LoginSigbUpCustomerNew.mytoken;
           Bundle parameters = new Bundle(1);
           parameters.putString("access_token", accessToken);
           return parameters;
    }

Here, I am getting response like this :-

{Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}

I cant understand why the data array is empty. Also I am requesting the user_photos permission from user. So where the thing is wrong. I am new to this Facebook SDK and dont know how to solve this. If anyone can help me it would be great.

Thanks in advance.

Upvotes: 2

Views: 1398

Answers (2)

kevto
kevto

Reputation: 549

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:

...
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
    session.openForRead(new Session.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.NewPermissionsRequest newPermissionsRequest = new Session
      .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

Upvotes: 1

Tobi
Tobi

Reputation: 31479

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}

Upvotes: 1

Related Questions