从Facebook的登录获取用户的电子邮件ID设置权限(Set permission for get

2019-09-01 16:42发布

我使用的是Facebook 3.0 SDK为Android。 我要实现Facebook登录。我喜欢访问的名称,用户ID用户的基本信息。 但我想有机会也通过电子邮件发送给用户的。 我经历了许多博客和论坛走了,但无法弄清楚如何做到这一点。

我用我自己的Android按钮登录不com.facebook.widget.LoginButton 。 如果我使用Facebook按钮日志很容易只需要使用这些行:

authButton.setReadPermissions(Arrays.asList("basic_info","email"));

但是,我有我自己的要求,就不得不去与Android默认的按钮。 以下是我迄今所做的:

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

我能够得到所有那些我在日志中我打印的东西,但无法从用户配置文件检索电子邮件。

我可以为此做些什么?

任何形式的帮助将不胜感激。

Answer 1:

解决了这个问题:这是我做了什么?

@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);

                                    }
                                }
                            });
                }
            }
        });
    }
}

现在,我能够得到电子邮件的权限与发布许可的休息。

这个标记为接受的回答,以便它可以帮助别人。



Answer 2:

TL; DR:使用类似这样获得的电子邮件:

String email = (String) user.getProperty("email");

我对你的代码的一个注释。 因为“电子邮件”是一个读取权限,你不需要创建一个单独的许可请求得到它。 你可以做这样的事情(未测试的代码,所以有可能是轻微的错误):

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

你可以摆脱在回调新的权限要求。



文章来源: Set permission for getting User's email ID from Facebook Login