如何在android应用程序中从facebook sdk获取电子邮件ID? [英] how to get email id from facebook sdk in android applications?
问题描述
我在我的 android 应用程序中集成了 Facebook 登录.我想获取登录用户的电子邮件 ID.我将如何获得它?
I integrated Facebook login in my android application. I want to get email id of login user. How will I get it?
private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
try {
userID =user.getId();
provider="facebook";
userName=user.getUsername();
firstName=user.getFirstName();
lastName=user.getLastName();
Log.d("****User****", "details:" + user);
}catch(Exception e){
这是我的代码.我使用 Request.GraphUserCallback()
方法,但没有来自此方法的电子邮件回复.
Here is my code. i use Request.GraphUserCallback()
method but there is no response of email from this method.
推荐答案
在调用 Session.openActiveSession
之前这样做以获得权限添加:
Before calling Session.openActiveSession
do this to get permissions add this:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
Session.openActiveSession()
中的最后一个参数应该是 permissions
.现在您可以访问user.getProperty("email").toString()
.
The last parameter in Session.openActiveSession()
should be permissions
.
Now you can access user.getProperty("email").toString()
.
这是我做facebook授权的方式:
This is the way I am doing facebook authorization:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
loginProgress.setVisibility(View.VISIBLE);
//start Facebook session
openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
//make request to the /me API
Log.e("sessionopened", "true");
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String firstName = user.getFirstName();
String lastName = user.getLastName();
String id = user.getId();
String email = user.getProperty("email").toString();
Log.e("facebookid", id);
Log.e("firstName", firstName);
Log.e("lastName", lastName);
Log.e("email", email);
}
}
});
}
}
}, permissions);
将此方法添加到您的活动中:
Add this method to your activity:
private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) {
Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
这篇关于如何在android应用程序中从facebook sdk获取电子邮件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!