19

嗨,我正在玩新的 Facebook Android SDK。我不知道的一件事是我如何要求电子邮件地址。我尝试了以下操作,但它返回了除电子邮件地址之外的所有字段的值。我想我可能必须单独请求电子邮件地址的许可,但不确定如果我使用 LoginFragment 该怎么做。

Request request = Request.newMeRequest(session, new GraphUserCallback() {

    @Override
    public void onCompleted(GraphUser user, Response response) {
        profilePictureView.setUserId(user.getId());
        userNameView.setText(user.getName());
    }
});

String NAME = "name";
String ID = "id";
String PICTURE = "picture";
String EMAIL = "email";
String FIELDS = "fields";
String REQUEST_FIELDS = TextUtils.join(",", new String[] {
    ID, NAME, PICTURE, EMAIL
});

Bundle parameters = new Bundle();
parameters.putString(FIELDS, REQUEST_FIELDS);
request.setParameters(parameters);
Request.executeBatchAsync(request);

任何帮助表示赞赏。谢谢

4

4 回答 4

30

请执行下列操作:

如上所述设置权限后,通过以下方式访问电子邮件:user.asMap().get("email"));

请参阅下面的示例

@Override
protected void onSessionStateChange(SessionState state, Exception exception) {

    // user has either logged in or not ...
    if (state.isOpened()) {
        // make request to the /me API
        Request request = Request.newMeRequest(this.getSession(),
                new Request.GraphUserCallback() {
                    // callback after Graph API response with user object

                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {
                        // TODO Auto-generated method stub
                        if (user != null) {
                            TextView etName = (TextView) findViewById(R.id.etName);

                            etName.setText(user.getName() + ","
                                    + user.getUsername() + ","
                                    + user.getId() + "," + user.getLink()
                                    + "," + user.getFirstName()+ user.asMap().get("email"));

                        }
                    }
                });
        Request.executeBatchAsync(request);
    }
}
于 2012-11-12T09:55:35.563 回答
24

当您打开会话时,请尝试在权限列表中包含“电子邮件”。

如果您使用的是 LoginButton,请执行以下操作:

loginButton.setReadPermissions(Arrays.asList("email"));

如果您自己打开会话,请执行以下操作:

session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));

您可以使用以下方法尝试请求/权限:https ://developers.facebook.com/tools/explorer/

使用这些权限成功登录后,请再次尝试请求,包括电子邮件字段。

于 2012-11-03T04:05:47.877 回答
15

如果您正在使用com.facebook.widget.LoginButton,您应该设置此片段的权限,之后 facebook 返回电子邮件。这是工作代码:

LoginButton authButton = findViewById(R.id.sign_in_facebook);
    List<String> permissions = new ArrayList<>();
    permissions.add("public_profile");
    permissions.add("email");
    permissions.add("user_birthday");
    authButton.setReadPermissions(permissions);`

编辑 Facebook 更新了它的 sdk,这促进了它的工作。从 4.x 版本(changelog)开始,您可以在需要它的不同地方初始化 sdk,而不是以非常简单的方式使用它。这是示例:

FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
CallbackManager callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    String email = object.optString("email");
                                    String uid = object.optString("id");
                                    loginPresenter.loginBySocial(email, uid);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "email");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                  //your code
                }

                @Override
                public void onError(FacebookException exception) {
                  //your code
                }
            });
于 2015-03-04T14:03:48.230 回答
1

我找到了一个解决方案:它有效

LoginFragment loginFragment = new LoginFragment() {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v = super.onCreateView(inflater, container, savedInstanceState);
                setReadPermissions(Arrays.asList("email"));
                return v;
            }
        };
于 2012-11-03T13:55:19.360 回答