简体   繁体   English

如何从Android应用程序中的Facebook sdk获取电子邮件ID?

[英]how to get email id from facebook sdk in android applications?

I integrated Facebook login in my android application. 我在我的Android应用程序中集成了Facebook登录。 I want to get email id of login user. 我想获取登录用户的电子邮件ID。 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){

Here is my code. 这是我的代码。 i use Request.GraphUserCallback() method but there is no response of email from this method. 我使用Request.GraphUserCallback()方法,但此方法没有电子邮件的响应。

Before calling Session.openActiveSession do this to get permissions add this: 在调用Session.openActiveSession之前,请执行此操作以获取权限添加:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

The last parameter in Session.openActiveSession() should be permissions . Session.openActiveSession()的最后一个参数应该是permissions Now you can access user.getProperty("email").toString() . 现在您可以访问user.getProperty("email").toString()

EDIT: 编辑:

This is the way I am doing facebook authorization: 这是我正在进行Facebook授权的方式:

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;
}

Following the suggestion of Egor N, I change my old code 根据Egor N的建议,我改变了我的旧代码

Session.openActiveSession(this, true, statusCallback);

whith this new one: 这个新的:

List<String> permissions = new ArrayList<String>();
permissions.add("email");
Session.openActiveSession(this, true, permissions, statusCallback);

Now FB ask the user about the permission of email and I can read it in the response. 现在FB询问用户有关电子邮件的许可,我可以在回复中阅读。

Try this article. 试试这篇文章。 Hope your issues will be solved Click Here 希望您的问题能够得到解决点击此处

Btw, You need to use user.asMap().get("email").toString()); 顺便说一下,你需要使用user.asMap().get("email").toString()); for receving the User Email ID. 用于接收用户电子邮件ID。 Also, you need to assign that into some of label like lblEmail.setText where lblEmail is a Textview. 此外,您需要将其分配到某些标签,如lblEmail.setText ,其中lblEmail是Textview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM