namrathu1
namrathu1

Reputation: 347

In Android how to add user_photos permission to get the facebook album photos?

I am using the facebook android sdk to access the user's facebook albums and intern get the photos. But when I do a "https://graph.facebook.com/"+wallAlbumID+"/photos?access_token="+facebook.getAccessToken() it returns blank data. { "data": [] }

I read in stackoverflow question and also in Graph API for Photo that I need to give user_photospermission while creating the facebook object. I am not sure how to do so.I read a lot of forums and also checked on SOF but could not find the solution.

This is how I am creating the facebook object

facebook = new Facebook(APP_ID);

Can anyone please help me with this.

Upvotes: 3

Views: 4312

Answers (2)

IgorGanapolsky
IgorGanapolsky

Reputation: 26824

You need to use Facebook SDK 3.0+. Also, enable the OAuth client login flow in the developer settings on the FB site. Then you can use the following code:

  1. First, log the user into FB inside of your app to get a Session object, then request permissions for user_photos if not granted yet:

    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {
    
        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                // check for permission to see user photos
                if (!hasPhotoPermissions())
                    session.requestNewPublishPermissions(new Session.NewPermissionsRequest(MyActivity.this, "user_photos"));
    
                else {
                    // photos permission granted, fetch user's albums
                    fetchAlbumsFromFB(session);
                }
            }
        }
    });
    
  2. Here is what hasPhotoPermissions() method looks like:

    private boolean hasPhotoPermissions() {
        Session session = Session.getActiveSession();
        return session.getPermissions().contains("user_photos");
    }
    
  3. Your app will then present the photos permissions screen to the user for approval.

Upvotes: 2

RobStemen
RobStemen

Reputation: 377

You need to create an ArrayList of permissions you want to access. For example, when I did it, when declaring variables.

private static final String[] PERMISSIONS = { "user_photos" };

Then, when implementing the user login area:

if (!facebook.isSessionValid()) {
    facebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}

Hope this helps!

Upvotes: 2

Related Questions