简体   繁体   English

登录后的Android facebook获取用户信息

[英]Android facebook after login get user information

I am trying to get the user information after login with facebook. 我想用facebook登录后获取用户信息。

    callbackManager = CallbackManager.Factory.create();
    loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_birthday", "email");
    final Context contextM = this.getApplicationContext();


    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            String firstName = Profile.getCurrentProfile().getFirstName();
            String lastName = Profile.getCurrentProfile().getLastName();
            String url = Profile.getCurrentProfile().getProfilePictureUri(100, 100).toString();

            final ImageView mImageView = (ImageView) findViewById(R.id.imageView);
            ImageRequest request = new ImageRequest(url,
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bitmap) {
                            mImageView.setImageBitmap(bitmap);
                        }
                    }, 0, 0, null,
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                        }
                    });
            MySingleton.getInstance(contextM).addToRequestQueue(request);
        }

and this is the error I get : 这是我得到的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getFirstName()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.facebook.Profile.getFirstName()'

It seems you have missed public_profile permission which alllows you to get both first_name as well as last name. 您似乎错过了public_profile权限,它允许您同时获取first_name和姓氏。

Just check what all permission you are granted using getRecentlyGrantedPermissions(). 只需使用getRecentlyGrantedPermissions()检查您获得的所有权限。

I have accessed results as follows using graph request. 我使用图形请求访问了如下结果。 check if this helps you 检查这是否对您有所帮助

LoginManager.getInstance().registerCallback(callbackManager,
        new FacebookCallback<LoginResult>(){
        @Override
        public void onSuccess(final LoginResult loginResult){
                GraphRequest request=GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback(){
        @Override
        public void onCompleted(
                JSONObject object,
                GraphResponse response){
                if(response.getError()!=null){
                // handle error
                }else{
                //**access details as follows**
                String email=object.optString("email");
                String first_name=object.optString("first_name");
                String last_name=object.optString("last_name");

                }
                });

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

        @Override
        public void onCancel(){
                //Toast.makeText(context, "Something went wrong. Please try again",
                // Toast.LENGTH_SHORT).show();
                }

        @Override
        public void onError(FacebookException exception){
                //Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                exception.printStackTrace();
                }
});

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

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