Get Date of Birth and Town/City from Facebook SDK GraphUserCallback

南楼画角 提交于 2019-12-23 19:27:20

问题


I have facebook loggin set up within my app like below, the idea is to login and then do a GraphUserCallback so we can get the use information and then pass to my own register system so a user account can be set up for my app.

Request.executeMeRequestAsync(session,
                new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {

                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                        if (user != null) {
                            Map<String, Object> responseMap = new HashMap<String, Object>();
                            GraphObject graphObject = response
                                    .getGraphObject();
                            responseMap = graphObject.asMap();
                            Log.i("FbLogin", "Response Map KeySet - "
                                    + responseMap.keySet());

                            Session session = Session.getActiveSession();
                            String email = null;
                            String token = null;
                            String username = null;
                            String gender = null;
                            String birthday = null;
                            if (responseMap.get("email") != null) {
                                email = responseMap.get("email").toString();
                                username = responseMap.get("first_name").toString()+" "+responseMap.get("last_name").toString();
                                gender = responseMap.get("gender").toString();
                                token = session.getAccessToken();
                                /*birthday = responseMap.get("user_birthday").toString();

                                Context context = getApplicationContext();
                                int duration = Toast.LENGTH_SHORT;

                                Toast toast = Toast.makeText(context, birthday, duration);
                                toast.show();*/

                                Intent i = new Intent(LogIn.this, FbLogin2Activity.class);
                                i.putExtra("Email", email);
                                i.putExtra("Token", token);
                                i.putExtra("Username", username);
                                i.putExtra("Gender", gender);
                                startActivity(i);
                            } else {
                                // Clear all session info & ask user to
                                // login again
                                if (session != null) {
                                    session.closeAndClearTokenInformation();
                                }
                            }
                        }
                    }
                });

When the user click a 'login' button:

fblogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Session session = Session.getActiveSession();
                if (session == null) {
                    Session.openActiveSession(LogIn.this, true,
                            statusCallback);
                } else if (!session.isOpened()) {
                    session.openForRead(new Session.OpenRequest(
                            LogIn.this).setCallback(statusCallback)
                            .setPermissions(permissions)
                            );
                }
                /*Intent i = new Intent(getApplicationContext(), FbLoginActivity.class);
                startActivity(i);*/
            }
        });

and my permissions are:

/***** FB Permissions *****/
permissions = new ArrayList<String>();
permissions.add("email");
permissions.add("user_birthday");
permissions.add("user_location");
/***** End FB Permissions *****/

The users email is returned and works however neither the users email or location is in the graphusecallback response.

Not sure if I have posted all that is needed so let me know but I cant work out why its not returning everything


回答1:


The user's birthday will be in the birthday field, and the user's location will be in the location field (as opposed to user_birthday or user_location). So

birthday = responseMap.get("birthday").toString();
location = responseMap.get("location").get("name").toString();

should work.



来源:https://stackoverflow.com/questions/19458180/get-date-of-birth-and-town-city-from-facebook-sdk-graphusercallback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!