Facebook android sdk 4. * - 如何获取登录用户的电子邮件?

时间:2015-05-27 16:09:00

标签: android facebook

  1. 我尝试在登录后从登录用户那里收到电子邮件,因为我想检查用户是否已经在我的数据库上收到了他的电子邮件。

  2. 但我不知道如何收到电子邮件。我使用getName从用户那里获取名称,我希望以类似的方式获取用户的电子邮件。

  3. 我的活动代码:

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            FacebookSdk.sdkInitialize(getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
    
            setContentView(R.layout.activity_main);
    
            info = (TextView) findViewById(R.id.info);
            loginButton = (LoginButton) findViewById(R.id.login_button);
    
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Profile profile = Profile.getCurrentProfile();
                    info.setText(message(profile));
    
                    String userId = loginResult.getAccessToken().getUserId();
    
                }
    
                @Override
                public void onCancel() {
                    info.setText("Login attempt cancelled.");
                }
    
                @Override
                public void onError(FacebookException e) {
                    e.printStackTrace();
                    info.setText("Login attempt failed.");
                }
            });
        }
    

    简历

    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        info.setText(message(profile));
    }
    

    关于活动结果

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    
        private String message(Profile profile) {
            StringBuilder stringBuffer = new StringBuilder();
            if (profile != null) {
                stringBuffer.append("Welcome ").append(profile.getName());
            }
            return stringBuffer.toString();
        }
    }
    

    感谢。

3 个答案:

答案 0 :(得分:0)

我认为您需要获得访问用户电子邮件的权限。尝试这样的事情。

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

然后看看您是否可以访问该电子邮件。

String email = profile.getProperty("email").toString();

一切顺利。

答案 1 :(得分:0)

要访问“电子邮件”,您必须为其设置权限。

//FB Login
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));

然后您可以在onSuccess中访问它。

答案 2 :(得分:0)

如果是适用于Android的Facebook SDK,则需要在4.2.0版中执行此操作:

    GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted( JSONObject object,  GraphResponse response) {
                    Log.d("onCompleted",object.toString());
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "email");
    request.setParameters(parameters);
    request.executeAsync();

此代码应放入onSucess()回调方法。