Facebook Android SDK获得用户的性别,电子邮件,封面,时区

时间:2017-06-08 05:46:32

标签: android facebook facebook-graph-api

我正在使用Facebook Android SDK 4+并且能够成功登录但我如何获得用户性别,电子邮件,封面,时区。这在Javascript API中可用。

loginButton.setReadPermissions("public_profile");

要获取名字和姓氏,请使用Facebook文档中提到的这两种方法。

名字:profile.getFirstName() 姓氏:profile.getFirstName()

获取性别,电子邮件,封面,时区。

我尝试使用

String get_gender = (String) profile.getProperty("gender");
String get_email = (String) profile.getProperty("email_address");
String get_cover = (String) profile.getProperty("cover");
String get_tz = (String) profile.getProperty("timezone");

但它不起作用 (Error: Cannot resolve method getProperty(java.lang.String))

需要帮助才能解决此问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个Facebook Graph API:

private void faceBookLigin() {

        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_photos", "email", "user_birthday", "user_friends", "user_location"));
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        accessToken = loginResult.getAccessToken();

                                /* make the API call */
                        GraphRequest request = GraphRequest.newMeRequest(
                                accessToken,
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object, GraphResponse response) {
                                        if (object == null) {
                                            cUtils.Toast("Please try again!", getApplicationContext());
                                        } else {
                                            try {
                                                String str_datestr = object.getString("birthday");
                                                String strd = str_datestr.replaceAll("\\\\/", "/");
                                                String str_date = strd.replaceAll("/", "-");
                                                DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault());
                                                Date date = null;
                                                try {
                                                    date = formatter.parse(str_date);
                                                } catch (java.text.ParseException e) {
                                                    // TODO Auto-generated catch block
                                                    e.printStackTrace();
                                                }
                                                long output = 0;
                                                if (date != null) {
                                                    output = date.getTime() / 1000L;
                                                }
                                                String str = Long.toString(output);
                                                actualTime = Long.parseLong(str);
//                                                Log.v("actualTime = ", " " + actualTime);
//                                                Log.v("Email = ", " " + object);
                                                email = object.getString("email");
                                                facebook_id = object.getString("id");
                                                first_name = object.getString("first_name");
                                                last_name = object.getString("last_name");
                                                user_location = object.getJSONObject("location");
                                                gender = object.getString("gender");
                                                verified = object.getBoolean("verified");
                                                id = object.getString("id");

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

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

                        if (accessToken != null)
                            LoginManager.getInstance().logOut();
                    }

                    @Override
                    public void onCancel() {
                        // App code
//                        Log.e(TAG, "facebook login canceled");
                        utils.cToast("facebook login canceled", getApplicationContext());
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
//                        Log.e(TAG, "facebook login failed error" + exception);
                        utils.cToast("facebook login failed", getApplicationContext());
                    }
                });
    }