Android Facebook Login API仅返回名称和ID

时间:2018-02-15 15:40:56

标签: java android facebook facebook-graph-api facebook-android-sdk

您好我在Android应用程序中使用Facebook图形Api,我能够提交请求并获得响应,但我的范围是"email", "user_birthday", "public_profile",它只返回名称和ID。

private void onFblogin()  {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList( "email", "user_birthday",  "public_profile"));

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

                        System.out.println("Success");
                        GraphRequest.newMeRequest(
                                loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject json, GraphResponse response) {
                                        if (response.getError() != null) {
                                            // handle error
                                            System.out.println("ERROR");
                                        } else {
                                            System.out.println("Success");
                                            try {

                                                String jsonresult = String.valueOf(json);
                                                System.out.println("JSON Result"+jsonresult);

                                                String name = json.getString("name");
                                                String str_email = json.getString("email");
                                                String str_id = json.getString("id");


                                                //boolean loggedIn = AccessToken.getCurrentAccessToken() == null;
                                                //get profile data
                                              //  LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
                                                String str_firstname = json.getString("first_name");
                                                String str_lastname = json.getString("last_name");

                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                }).executeAsync();

                    }

                    @Override
                    public void onCancel() {
                        Log.d("facebook Login","On cancel");
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.d("facebook Login",error.toString());
                    }
                });
    }


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        } else if(requestCode == FB_SIGN_IN){
            callbackmanager.onActivityResult(requestCode, resultCode, data);
        }
    }

摇篮

implementation 'com.facebook.android:facebook-login:[4,5)'

enter image description here

有人可以帮助我获取public_profile中的所有信息

1 个答案:

答案 0 :(得分:2)

您没有指定任何字段,请查看文档:{​​{3}}

例如:

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // Application code
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,birthday");
request.setParameters(parameters);
request.executeAsync();

如果不指定fields参数,则只会在响应中获得一些默认字段。适用于所有API调用。

查看文档中的“选择字段”:https://developers.facebook.com/docs/android/graph