Getting Only Id and Name in jsonObject during Face

2019-07-16 04:36发布

问题:

I am trying to login from my app using facebook login but what I am getting in response in jsonObject is only id and name and nothing else when i request loginButton.setReadPermissions("public_profile"). Can anyone figure out what is the problem ? Here is my code.

public class LoginActivity extends AppCompatActivity {
LoginButton loginButton;
CallbackManager callbackManager;
String id, name, gender, ageRange;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_login);
    loginButton=(LoginButton)findViewById(R.id.login_button);
    callbackManager= CallbackManager.Factory.create();
    loginButton.setReadPermissions("public_profile");
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken=loginResult.getAccessToken();
            GraphRequest graphRequest= GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                    if (jsonObject!=null)
                    {
                        Log.e("JSON Object",jsonObject.toString());
                        Log.e("GraphResponse",graphResponse.toString());
                        try
                        {
                            id=jsonObject.getString("id");
                            name=jsonObject.getString("name");
                            gender=jsonObject.getString("gender");
                            ageRange=jsonObject.getString("age_range");
                        }
                        catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            });
            graphRequest.executeAsync();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(),"Cancelled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(),"Error while Connecting", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

回答1:

Using graph API go with below code.

get all values whatever you want just only pass parameter like

parameters.putString("fields", "id,name,link,email,first_name,last_name,gender");

=============================

Full code

public static CallbackManager callbackManager;
Button btn_login;

oncreate

FacebookSdk.sdkInitialize(getApplicationContext());

    btn_login = (Button) findViewById(R.id.btn_facebook);


    btn_login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            callbackManager = CallbackManager.Factory.create();
            // loginButton.setReadPermissions(Arrays.asList("user_status"));

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




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

                            System.out.println("Success");



                            GraphRequest request = 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 str_email = json
                                                            .getString("email");
                                                    String str_id = json
                                                            .getString("id");
                                                    String str_firstname = json
                                                            .getString("first_name");
                                                    String str_lastname = json
                                                            .getString("last_name");

                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    });
                            Bundle parameters = new Bundle();
                            parameters.putString("fields", "id,name,link,email,first_name,last_name,gender");
                            request.setParameters(parameters);
                            request.executeAsync();


                        }

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

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

        }
    });

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}

manifest

  <!-- facebook -->

    <activity
        android:name="com.facebook.FacebookActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <!-- facebook finish -->

Refrence link : https://developers.facebook.com/docs/android/graph

Thanks.



回答2:

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

V2.4 permissions

V2.4 Doc android login