为什么我不能从 GraphRequest 中获取用户的名字?

Why can't I get a user's first name from a GraphRequest?

我希望用户能够使用 Facebook SDK 注册我的 Android 应用程序。为了获取他们的信息,我正在使用权限“email”和“public_profile”执行 GraphRequest。根据 this page,用户的名字和姓氏应该可以在“first_name”和“last_name”字段中访问。但是,当我单击该按钮时,出现错误提示 first_name 没有值。返回的 JSON 数组符合以下行:

{"name":"First M. Last","id":"1234567890123456"}

我还没有提交我的应用程序以供审核,因为它还没有完成。这可能与此问题有关吗?

根据官方文档,您访问 public_profile 权限的登录名应如下所示:

 LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email"));

获取个人资料信息:

 private void getFbDetails(final AccessToken accessToken) {
        GraphRequest request = GraphRequest.newMeRequest(accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {

                     //   Toast.makeText(Login.this, object.toString(), Toast.LENGTH_LONG).show();
                        Log.v("FB Details", object.toString());
                        if (object != null) {
                            name_fb = object.optString("name");
                            email_fb = object.optString("email");
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,first_name, last_name, email,link");
        request.setParameters(parameters);
        request.executeAsync();
    }

您可以获得名字和姓氏作为单独的参数。您可以连接名字和姓氏以传入 api。我在使用 Facebook 登录时使用此方法获取用户详细信息。

            // initalize the login button
         loginButton = (LoginButton) findViewById(R.id.login_button);
           loginButton.setReadPermissions("public_profile");
           loginButton.setReadPermissions("email");


         loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
           @Override
           public void onSuccess(LoginResult loginResult) {
      raphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

                      @Override
                      public void onCompleted(JSONObject object, GraphResponse response) {

                          // Get facebook data from login
                          try {
                              Bundle bFacebookData = getFacebookData(object);
                              usernamestr = fbEmail;

                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                  });
                 Bundle parameters = new Bundle();
               parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
               request.setParameters(parameters);
               request.executeAsync();
                }
                 @Override
           public void onCancel() {

               Toast.makeText(Login.this, "Login attempt canceled", Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onError(FacebookException e) {
                              Toast.makeText(Login.this, "Login attempt failed", Toast.LENGTH_SHORT).show();
           }
       });

        private Bundle getFacebookData(JSONObject object) throws JSONException {

          Bundle bundle = new Bundle();
          String id = object.getString("id");

          try {
              URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");

              bundle.putString("profile_pic", profile_pic.toString());

          } catch (MalformedURLException e) {
              e.printStackTrace();
              return null;
          }

          bundle.putString("idFacebook", id);

          fbEmail = object.getString("email");
          fbId = object.getString("id");
          if (object.has("first_name"))
              bundle.putString("first_name", object.getString("first_name"));
          if (object.has("last_name"))
              bundle.putString("last_name", object.getString("last_name"));
          if (object.has("email"))
              bundle.putString("email", object.getString("email"));
          if (object.has("gender"))
              bundle.putString("gender", object.getString("gender"));
          if (object.has("birthday"))
              bundle.putString("birthday", object.getString("birthday"));
          if (object.has("location"))
              bundle.putString("location", object.getJSONObject("location").getString("name"));

          return bundle;
      }