如何从最新的Facebook sdk-4.0.1获取用户信息

时间:2015-05-30 13:03:46

标签: android facebook

如何从最新的Facebook sdk-4.0.1获取用户信息,如:

电子邮件 - user_birthday - user_friends

注意:我使用LoginButton

    final LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setReadPermissions("public_profile");
    loginButton.setReadPermissions("email");
    loginButton.setReadPermissions("user_birthday");

1 个答案:

答案 0 :(得分:2)

首先,可以在一行中设置权限:

loginButton.setReadPermissions(Arrays.asList("user_friends, public_profile, email, user_birthday"));

现在,考虑到您以这种方式注册回调:

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) { }

        @Override
        public void onCancel() { }

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

将此代码放在onSuccess()方法中:

        GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted( JSONObject object,  GraphResponse response) {
                        Log.d("onCompleted",object.toString());
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link, email");
        request.setParameters(parameters);
        request.executeAsync();

注意这一行:

parameters.putString("fields", "id,name,link, email");

...这些将是onCompleted()方法中JSONObject中返回的字段。

参考:

https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#reading https://developers.facebook.com/docs/graph-api/reference/user