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

瘦欲@ 提交于 2020-01-04 01:49:10

问题


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.


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/14408889/in-android-how-to-add-user-photos-permission-to-get-the-facebook-album-photos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!