设置从Facebooklogin获取用户电子邮件ID的权限

我使用的是Android Facebook 3.0 SDK 。 我必须实现Facebooklogin。我正在访问用户的基本信息,如名称,用户ID。 但我想要访问用户的电子邮件。 我经历了许多博客和论坛,但无法弄清楚如何做到这一点。

我正在使用我自己的androidbuttonlogin,而不是com.facebook.widget.LoginButton 。 如果我使用Facebookloginbutton,很容易只需要使用这些行:

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

但是,我有我自己的要求,将不得不去与默认的Androidbutton。 这是我迄今为止所做的:

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

我能够得到我正在打印的所有日志,但无法从用户configuration文件中检索电子邮件。

我能为此做些什么?

任何forms的帮助将不胜感激。

解决它:这是我所做的…

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

现在,我可以通过其他发布权限获得电子邮件许可。

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

TL; DR:使用这样的东西来获取电子邮件:

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

我有一个关于你的代码的评论。 由于“电子邮件”是一个读取权限,您不需要创build一个单独的权限请求来获取它。 你可以做这样的事情(代码未经testing,所以可能会有轻微的错误):

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

您可以在callback中删除新的权限请求。