简体   繁体   English

无法通过Android的Parse从Facebook登录获取性别

[英]Unable to get gender from Facebook login via Parse for Android

I followed this tutorial from parse and configured my FB developer account along with needed package name and hash key as per their guidelines. 我从解析开始遵循教程,并根据他们的指南配置了我的FB开发人员帐户以及所需的包名称和哈希键。 In permissions, I have asked for user profile and email id so I should get their facebook ID, full name, gender and email id as per facebook dcumentation . 在权限中,我询问了用户个人资料和电子邮件ID,因此我应该按照facebook的文档获取其Facebook ID,全名,性别和电子邮件ID。

I am getting this JSON returned. 我正在返回此JSON。 Gender and email id are not event featuring as key with null values. 性别和电子邮件ID不是具有空值键的事件。 I have covered my FB id of course. 我已经介绍了我的FB ID。 {"name":"Varun Agarwal","id":"11111111111111111"} {“名称”:“ Varun Agarwal”,“ id”:“ 11111111111111111”}

public void onLoginClick(View v) {
    progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);

    List<String> permissions = Arrays.asList("public_profile", "email");
    // NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
    // (https://developers.facebook.com/docs/facebook-login/permissions/)

    ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
      @Override
      public void done(ParseUser user, ParseException err) {
        progressDialog.dismiss();
        if (user == null) {
          Log.d(IntegratingFacebookTutorialApplication.TAG, "Uh oh. The user cancelled the Facebook login.");
        } else if (user.isNew()) {
          Log.d(IntegratingFacebookTutorialApplication.TAG, "User signed up and logged in through Facebook!");
          showUserDetailsActivity();
        } else {
          Log.d(IntegratingFacebookTutorialApplication.TAG, "User logged in through Facebook!");
          showUserDetailsActivity();
        }
      }
    });
  }

 private void makeMeRequest() {
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
              @Override
              public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                if (jsonObject != null) {
                  JSONObject userProfile = new JSONObject();
                    Log.d("Data", jsonObject.toString());

                  try {
                    userProfile.put("facebookId", jsonObject.getLong("id"));
                    userProfile.put("name", jsonObject.getString("name"));

                    if (jsonObject.getString("gender") != null)
                      userProfile.put("gender", jsonObject.getString("gender"));

                    if (jsonObject.getString("email") != null)
                      userProfile.put("email", jsonObject.getString("email"));

                    // Save the user profile info in a user property
                    ParseUser currentUser = ParseUser.getCurrentUser();
                    currentUser.put("profile", userProfile);
                    currentUser.saveInBackground();

                    // Show the user info
                    updateViewsWithProfileInfo();
                  } catch (JSONException e) {
                    Log.d(IntegratingFacebookTutorialApplication.TAG,
                            "Error parsing returned user data. " + e);
                  }
                } else if (graphResponse.getError() != null) {
                  switch (graphResponse.getError().getCategory()) {
                    case LOGIN_RECOVERABLE:
                      Log.d(IntegratingFacebookTutorialApplication.TAG,
                              "Authentication error: " + graphResponse.getError());
                      break;

                    case TRANSIENT:
                      Log.d(IntegratingFacebookTutorialApplication.TAG,
                              "Transient error. Try again. " + graphResponse.getError());
                      break;

                    case OTHER:
                      Log.d(IntegratingFacebookTutorialApplication.TAG,
                              "Some other error: " + graphResponse.getError());
                      break;
                  }
                }
              }
            });

    request.executeAsync();
  }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    makeMeRequest(); ---------> }

.Basically the name and some long fb id is all I am receiving. 我基本上只收到名称和一些长的fb id。 There is no gender or email appearing anywhere. 没有性别或电子邮件出现在任何地方。 The code above works if i comment out the parts related to gender and email since for gender==null to hold true, a key of "gender" must be declared as in {"gender":null} hence it is giving me issues until i comment out that part of the code. 如果我注释掉与性别和电子邮件相关的部分,则上面的代码有效,因为为使性别== null成立,必须在{“ gender”:null}中声明“ gender”的键,因此这给我带来了问题,直到我注释掉那部分代码。

Can someone suggest a solution please? 有人可以提出解决方案吗? This code, I have taken from their tutorial and read through the permissions given in FB documentation. 我从他们的教程中获取了这段代码,并仔细阅读了FB文档中提供的权限。 Its strange that the name is coming but not the gender as both are in public profile information. 奇怪的是,这个名字即将出现,但性别却没有出现,因为两者都在公共资料信息中。 I have checked my own FB account to make sure I have a gender set so that's not a problem. 我已经检查了自己的FB帐户,以确保设置了性别,这不是问题。

try this use graphResponse in place of jsonObject. 试试这个使用graphResponse代替jsonObject。

public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) 
{
    response.getJSONObject().getString("email")
    response.getJSONObject().getString("gender")
    response.getJSONObject().getString("id")
    response.getJSONObject().getString("name")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM