如何使用Parse Facebook for Android获取用户电子邮件地址

4

我正在尝试从ParseFacebook for Android获取用户的电子邮件地址,但是当我尝试获取电子邮件时,返回null。以下是我的代码:

//on Login:

List<String> permissions = Arrays.asList("email","basic_info", "user_about_me", "user_location");
        ParseFacebookUtils.logIn(permissions,SignUpActivity. this, new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException err) {
                SignUpActivity.this.progressDialog.dismiss();
                if(err!=null)
                Log.w("parse exception",""+err.getMessage());
                if (user == null) {
                Log.e("FB","Uh oh. The user cancelled the Facebook login.");
                } else if (user.isNew()) {
                    Log.w("FB","User signed up and logged in through Facebook!");
                    //showUserDetailsActivity();
                    getProfileInfo();
                } else {
                    Log.w("FB","User logged in through Facebook!");
                //  showUserDetailsActivity();
                    getProfileInfo();
                }
            }

        });

发起请求:

Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            JSONObject userProfile = new JSONObject();
                            try {
                                // Populate the JSON object
                                try {
                                    userProfile.put("facebookId", user.getId());
                                    userProfile.put("name", user.getName());
                                } catch (Exception e) {
                                    // TODO: handle exception
                                }
                                if (user.getProperty("email") != null) {
                                    userProfile.put("email",(String) user.getProperty("email"));
                                }
                                if (user.getLocation().getProperty("name") != null) {
                                    userProfile.put("location", (String) user.getLocation().getProperty("name"));
                                }
                                if (user.getProperty("gender") != null) {
                                    userProfile.put("gender",(String) user.getProperty("gender"));
                                }


                            } catch (JSONException e) {
                                Log.e("Facebook parsing data","Error parsing returned user data.");
                            }

                        } else if (response.getError() != null) {
                            if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY) || (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) {
                                Log.e("Facebook parsing data","The facebook session was invalidated.");
                                //onLogoutButtonClicked();
                            } else {
                                Log.e("Facebook parsing data","Some other error: "+ response.getError().getErrorMessage());
                            }
                        }
                    }
                });
        request.executeAsync();

我尝试了所有的方法,但无论我做什么都会得到"email null"的提示 :(

5个回答

0
在完成以上权限设置后,可以通过以下方式访问电子邮件:user.asMap().get("email"));

user.asMap().get("email")) 返回 null。 - Saad Asad

0

你应该会收到这样的电子邮件

String email = (String) response.getGraphObject().getProperty("email");
if(email!=null && !email.equals("")
//email variable should've email id.

仍然得到 null,虽然获取了所有其他字段。无法追踪错误... - Saad Asad

0
请添加您的请求:
 Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() 

在登录请求条件中 => user.isnew()

在编程方面相关的内容

else if (user.isNew()) {
                Log.w("FB","User signed up and logged in through Facebook!");
                //showUserDetailsActivity();
                getProfileInfo();
            }

在这之间编写您的代码块,您将能够获取包含电子邮件的新用户信息。

这在iOS上完美运行,并且肯定会对Android有所帮助。


0
尝试在登录调用中删除权限列表。
ParseFacebookUtils.logIn(SignUpActivity.this, new LogInCallback() {
//you can add permissions on later screens.
}

你能否请解释一下你在建议什么? - Saad Asad
登录时不要包括权限。 - nomi13ok

0

在这行代码之前

request.executeAsync();

添加这三行。

Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);

现在您将在响应中收到电子邮件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接