get user facebook permissions

2019-02-20 18:55发布

问题:

I want to add a facebook permission to my users. That is how I do it:

Session.openActiveSession(this, true, new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                List<String> PERMISSIONS = Arrays.asList("email");

                if (!session.getPermissions().containsAll(PERMISSIONS)) {
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(MainActivity.this,
                            PERMISSIONS);
                    session.requestNewReadPermissions(newPermissionsRequest);
                }

                Request.newMeRequest(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            Toast.makeText(MainActivity.this, user.asMap().get("email").toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                }).executeAsync();
            }
        }
    });

The problem is that I don't want to open a session every time, just if the user don't have this permission. Is there a way to locally know if the user has already approved this permission in my app?

回答1:

use below code

Session session = getActiveSession();
if(session.isOpened)
{
  List<String> permissions = session.getPermissions();
    if(!permissions.contain("email"))
    {
 Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(MainActivity.this,
                            PERMISSIONS);
                    session.requestNewReadPermissions(newPermissionsRequest);

}
}else
{
Session.openActiveSession(this, true,callback);
}