无法从Facebook的GraphRequest类中获取电子邮件

时间:2016-01-22 11:05:58

标签: android facebook-sdk-4.0

这是我的fbLoginFragment.java,其中我试图让用户使用Facebook SDK 4.9.0登录 - :

    LoginButton loginButton;
    AccessToken accessToken;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_fblogin, container, false);
    }

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;
    private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            // Application code
                            String email = object.optString("email");
                            Log.i("Radhe", response.toString());
                            Log.i("Radhe", object.toString() + "  " +email);

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

        @Override
        public void onCancel() {
            Log.i("Radhe", "Cancelled");
        }

        @Override
        public void onError(FacebookException e) {
            Log.i("Radhe", "Error = " + e);
        }
    };

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

            }
        };

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                Log.i("Radhe", "So Hari it is coming here");
            }
        };

        accessTokenTracker.startTracking();
        profileTracker.startTracking();
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile");
        loginButton.setReadPermissions("email");
        loginButton.setReadPermissions("user_birthday");
        loginButton.setReadPermissions("user_friends");
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, callback);

    }

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

        callbackManager.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onStop() {
        super.onStop();
        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }

    @Override
    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    private void displayMessage(Profile profile) {
        /****************************Save user to Parse***********************************/
        if (profile != null) {
            ParseUser user = new ParseUser();
            user.setUsername(profile.getId().toString());
            user.setPassword(profile.getId().toString());
            user.put("legalname", profile.getFirstName());
            user.put("surname", profile.getLastName());

            user.signUpInBackground(new SignUpCallback() {
                public void done(ParseException e) {
                    if (e == null) {
                        Log.i("Radhe", "Radhe! Parse signup is success");
                        startActivity(new Intent(getActivity().getApplicationContext(), home.class));
                    } else {
                        e.printStackTrace();
                        Log.i("Radhe", "Radhe! Parse signup is failure " + e);
                    }
                }
            });

        }
    }

我也给了权限,但我没有在JSON对象中收到电子邮件和出生日期。

这是我的Logcat的结果 - :

01-22 16:29:07.909 8841-8841/? I/Radhe: {Response:  responseCode: 200, graphObject: {"id":"820969881382796","gender":"male","name":"Pranav Shukla"}, error: null}
01-22 16:29:07.909 8841-8841/? I/Radhe: {"id":"820969881382796","gender":"male","name":"Pranav Shukla"}

1 个答案:

答案 0 :(得分:2)

您需要添加List权限,如下所示:

....
List<String> permissions = new ArrayList<String>();
permissions.add("public_profile");
permissions.add("email");
permissions.add("user_birthday");
permissions.add("user_friends");

loginButton.setReadPermissions(permissions);
....

检查Facebook文档:public void setReadPermissions(List permissions)