Set permission for getting User's email ID from Facebook Login
Solution 1:
Solved it: Here is what I have done...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLogin = (Button) findViewById(R.id.logIn);
mLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(context).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
// Do whatever u want. User has logged in
} else if (!currentSession.isOpened()) {
// Ask for username and password
OpenRequest op = new Session.OpenRequest((Activity) context);
op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
op.setCallback(null);
List<String> permissions = new ArrayList<String>();
permissions.add("publish_stream");
permissions.add("user_likes");
permissions.add("email");
permissions.add("user_birthday");
op.setPermissions(permissions);
Session session = new Builder(MainActivity.this).build();
Session.setActiveSession(session);
session.openForPublish(op);
}
}
public void call(Session session, SessionState state, Exception exception) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(context).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(final Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello "
+ user.getName() + "!");
access_token = session
.getAccessToken();
firstName = user.getFirstName();
fb_user_id = user.getId();
System.out
.println("Facebook Access token: "
+ access_token);
System.out.println("First Name:"
+ firstName);
System.out.println("FB USER ID: "
+ fb_user_id);
}
}
});
}
}
});
}
}
Now I'm able to get email permission with rest of the publish permission.
Marking this as accepted answer so that it can help others.
Solution 2:
TL; DR: use something like this to get the email:
String email = (String) user.getProperty("email");
I have one comment about your code. Since "email" is a read permission, you don't need to create a separate permission request to get it. You can do something like this (code untested, so there might be slight errors):
Session s = new Session(this);
Session.setActiveSession(s);
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("basic_info","email"));
request.setCallback( /* your callback here */ );
s.openForRead(request);
and you can get rid of the new permission request in your callback.