I'm using Facebook 3.0 SDK
for android. I have to implement Facebook
log in. I'm accessing user's basic info like name, userID. But I want to have access to email also of the user. I have gone through many blogs and forum but cannot figure out how to do that.
I'm using my own android button for log in not com.facebook.widget.LoginButton
. If I use Facebook
log in button it was easy just have to use these lines:
authButton.setReadPermissions(Arrays.asList("basic_info","email"));
But, I have my own requirement, will have to go with default android buttons. Here is what I have done so far:
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, Arrays.asList("email"));
@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) {
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(final Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
session.requestNewReadPermissions(newPermissionsRequest);
// make request to the /me API
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
System.out
.println("Facebook Response: "
+ response.toString());
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);
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
I'm able to get all those things which I'm printing in log, but was unable to retrieve email from the user profile.
What can I do for this?
Any kind of help will be appreciated.
More than just your photos and status updates may be shared with your friends on Facebook -- including other information such as your email address and phone number. If you do not want your friends or anyone else on Facebook to know your email address, you can hide it.
Facebook Login also enables you to ask for permissions when people log in to your app. These permissions, if granted by the user, give your app access to items of user data. For example, your app can access a user's name and profile photo.
From your Page, click Manage, then click Page Access. Next to People with task access, click Add New. Click Next, type the name or email address of the person you want to give task access to the Page, then click their name. Click to select the features you want this person to manage, then click Give Access.
Step 1: Open the Facebook website. Step 2: Click on the small down arrow at the top. Choose Settings from the menu. Step 3: The primary email will be listed under the General tab next to Contact.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With