简体   繁体   中英

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_photos permission 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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM