Android Facebook 4.2 API是否提供电子邮件ID&手机号码?

时间:2015-06-16 05:06:35

标签: android facebook

是否可以在我们的应用中获取使用Facebook登录的用户的电子邮件ID? 刚签出并发现除了电子邮件ID以外的所有内容都是JSON&图响应。

{"id":"1xxxxxxxxxxx","first_name":"xxxxxxx","timezone":5.5,"verified":true,"name":"xxxxxxxx","locale":"en_US","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/xxxxxxxxxxx\/","last_name":"xxx","gender":"xx","updated_time":"2014-06-26T06:06:41+0000"}

以下使用的示例代码来自facebook的开发者网站..

LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {

                        handlePendingAction();
                        updateUI();

                        GraphRequest request = GraphRequest.newMeRequest(
                                AccessToken.getCurrentAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object,
                                            GraphResponse response) {
                                        // String email = object
                                        // .getString("email");
                                        System.out.println(object.toString()
                                                + " :user email: "
                                                + response.toString());

                                    }

                                });                         
                        request.executeAsync();

                    }

                    @Override
                    public void onCancel() {
                        if (pendingAction != PendingAction.NONE) {
                            showAlert();
                            pendingAction = PendingAction.NONE;
                        }
                        updateUI();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        if (pendingAction != PendingAction.NONE
                                && exception instanceof FacebookAuthorizationException) {
                            showAlert();
                            pendingAction = PendingAction.NONE;
                        }
                        updateUI();
                    }

                    private void showAlert() {
                        new AlertDialog.Builder(
                                HelloFacebookSampleActivity.this)
                                .setTitle(R.string.cancelled)
                                .setMessage(R.string.permission_not_granted)
                                .setPositiveButton(R.string.ok, null).show();
                    }
                });

3 个答案:

答案 0 :(得分:2)

您可以按如下方式获取已记录的用户电子邮件,但请注意,

  1. 他们不保证您会在此处阅读电子邮件地址。 https://developers.facebook.com/docs/facebook-login/permissions/v2.3#reference-public_profile

  2. 在某些情况下,虽然用户提供了电子邮件,但如果电子邮件无效,则不会通过请求。

  3. 您在登录用户时必须申请“电子邮件”权限。即使您的应用具有电子邮件权限,也不会始终返回电子邮件地址,因此您的应用不应依赖该字段始终拥有价值如下。

  4. LoginManager.getInstance()。logInWithReadPermissions(this,Arrays.asList(“public_profile”,“user_friends”,“email”));

答案 1 :(得分:1)

您需要添加&#34;电子邮件&#34;。

的权限
private static final String[] PERMISSIONS = new String[] {
    "publish_actions", "email", "user_birthday","read_stream", "user_photos"
};


LoginManager.getInstance().logInWithReadPermissions(this, PERMISSIONS );

答案 2 :(得分:1)

尝试请求权限,然后这样的事情应该有效:

public void retrieveEmailUsingGraphAPI(final AccessToken accessToken) {
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
  @Override
  public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
    if (jsonObject != null && jsonObject.has(EMAIL)) {
      try {
        String userEmailFromFacebook = jsonObject.getString(EMAIL);

      } catch (JSONException jsonException) {
        LogE(TAG, "FacebookGraphJSONCallback onCompleted ", jsonException);
      }
    } else {
      LogE(TAG, "An error happened in the GraphRequest.GraphJSONObjectCallback the jsonObject is null");
    }
  }  
});

  Bundle parameters = new Bundle();
  parameters.putString("fields", EMAIL);
  request.setParameters(parameters);
  request.executeAsync();
}

EMAIL是一个字符串=“电子邮件”。