繁体   English   中英

有谁知道如何在Android平台上从Facebook SDK获取名字,姓氏,电子邮件,ID,生日,性别,家乡,年龄等?

[英]Does anyone know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?

我是Facebook SDK的新手。 有谁知道如何在Android平台上从Facebook SDK获取名字,姓氏,电子邮件,ID,生日,性别,家乡,年龄等?

请给我看看代码?

以下是我尝试过的

callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions( "public_profile", "email", "user_birthday", "user_friends");


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = AccessToken.getCurrentAccessToken();
            boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

            GraphRequest request = GraphRequest.newMeRequest(
                    accessToken,
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            // Insert your code here
                            try {
                                if (object != null) {
                                    JSONObject obj = response.getJSONObject();
                                     id = obj.getString("id");
                                     name = obj.getString("name");
                                     birthday = obj.getString("birthday");
                                     email = obj.getString("email");
                                    String gender = obj.getString("gender");
                                     showUserInfoToast();

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

                        }
                    });

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


        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });

我试过了,但是不行。

请尝试使用此代码,希望它将获取您需要的所有数据。

 public void onSuccess(final LoginResult loginResult) {
    Log.e("bug", "facebook sign in success");

    if (loginResult != null) {

        Profile profile = Profile.getCurrentProfile();

        if (profile != null) {
            firstName = profile.getFirstName();
            lastName = profile.getLastName();
            social_id = profile.getId();
            profileURL = String.valueOf(profile.getProfilePictureUri(200, 200));
            Log.e(TAG, "social Id: " + profileURL);
        }

        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                Log.e("bug", "facebook social_id=>" + social_id + " toeken" + loginResult.getAccessToken().getToken());
                Log.e("bug", "graph response=>" + object.toString());
                try {
                    if (object.has("name")) {
                        String[] name = object.getString("name").split(" ");
                        firstName = name[0];
                        lastName = name[1];
                    }
                    email = object.has("email") ? object.getString("email") : "";
                    social_id = object.has("id") ? object.getString("id") : "";
                    socialSignUp("facebook");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

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

您访问的数据取决于某人授予您的应用程序的权限以及他们选择与应用程序共享的数据( 链接

这些字段可以为null

要获取除userIdnameemail之外的任何user-specific datauser-specific data ,您必须将应用程序提交给Facebook控制台进行审核,以便使Facebook可以清楚地知道您想对用户的个人数据做什么。

如果您的应用程序使用此数据进行某种有用的操作,那么facebook将允许您访问该数据,例如生日,user_friends和用户性别。 为此,请转到Facebook开发者控制台,然后按照该过程操作。 Facebook流

希望这可以帮助您实现想要的目标。 看到这个

暂无
暂无

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

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