0

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?

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
roiberg
  • 13,629
  • 12
  • 60
  • 91

1 Answers1

0

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);
}
Madhu
  • 298
  • 1
  • 12
  • How will that be open only if the user don't have email permission? – roiberg Sep 24 '13 at 11:46
  • !permissions.contain("email") condition checks whether user has email permission or not – Madhu Sep 24 '13 at 12:15
  • @roiberg i didnt get you. locally means?? – Madhu Sep 24 '13 at 12:19
  • Locally means that I don't want to open a session. It's a user that uses my app and already approved a permission, why do I need to "ask" facebook with a session every time? – roiberg Sep 24 '13 at 13:19
  • You have to open the session in order to do anything (this includes making graph API requests). Opening the session does not always require user interaction (usually only the first time, in subsequent times it's cached). So you *should not* be afraid to open a session. When you request new permissions, that usually WILL require user interaction, in which case you should use a similar pattern that Madhu pointed out above. – Ming Li Sep 26 '13 at 23:17