Set permission for getting User's email ID from Facebook Login
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.
Facebook oAuth: Force email permission
They are using an old App created before end of April 2014, it was different back then. You can´t force it anymore, you can only check if the user authorized the permissions after login, with the return_scopes
flag, for example:
https://developers.facebook.com/docs/reference/javascript/FB.login/v2.2
Facebook login not returning email id
To fetch not only email id, everything you can get.Here is code.
private CallbackManager callbackManager;
private LoginButton loginButton;
inside OnCreate() method
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
loginButton = (LoginButton) findViewById(R.id.login_button);
// don't forget to give this.
loginButton.setReadPermissions(Arrays.asList("public_profile,email,user_birthday"));
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDetails();
}
});
}
private void getDetails() {
//for facebook
// FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
//register callback object for facebook result
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
String facebook_id = profile.getId();
String f_name = profile.getFirstName();
String l_name = profile.getLastName();
profile_image = profile.getProfilePictureUri(400, 400).toString();
}
String email_id = jsonObject.getString("email"); //email id
} catch (JSONException e) {
Logger.logError(e);
}
}
});
Happy coding.
facebook api email permissions
If you go into your developer page and click on the app settings you can edit permissions the app requests. If you click in the User & Friend Permissions field and start typing email, you can tell your app to request email permissions. If you then click on the "Preview Login Dialogue" you will see it asks for exactly what you linked to.
Related Topics
How to Test If a Fragment View Is Visible to the User
Android - How to Check Proguard Obfuscation Has Worked
Android E/Parcel﹕ Class Not Found When Unmarshalling (Only on Samsung Tab3)
Android:How to Update the Selector(Statelistdrawable) Programmatically
Applying Effects on Video Being Played
Default Activity Not Found in Android Studio
How to Get Bitmap from a Url in Android
Android Gradle Build Error:(9, 0) Gradle Dsl Method Not Found: 'Compile()'
Java.Lang.Illegalargumentexception: Can Only Use Lower 16 Bits for Requestcode
Circular Dependencies Cannot Exist in Relativelayout, Android
How to Shutdown an Android Mobile Programmatically
Android Debug Monitor Hierarchy View Not Showing
When a Fragment Is Replaced and Put in the Back Stack (Or Removed) Does It Stay in Memory