简体   繁体   English

使用Facebook android SDK访问Facebook用户名

[英]Accessing Facebook username using Facebook android SDK

I am using facebook 3.15 android SDK. 我正在使用facebook 3.15 android SDK。 I am using following code to get the username and other details of the logged in user: 我正在使用以下代码来获取登录用户的用户名和其他详细信息:

private final String[] PERMISSIONS = new String[] { "public_profile", "email" };

//this function gets called when user clicks on facebook login button
public void onFbButtonClick(View v) {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList(PERMISSIONS)).setCallback(callback));
        } else {
            Session.openActiveSession(this, true, callback);
        }
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i("Logged in...");
        // make request to the /me API
        Request.newMeRequest(session, new Request.GraphUserCallback() {
            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
                    Log.d("user.getName : " + user.getName());
                    Log.d("user.getUsername : " + user.getUsername());
                    Log.d("user.getId : " + user.getId());
                    String email = user.getProperty("email").toString();
                    Log.d("user.email : " + email);
                }
            }
        }).executeAsync();
    } else if (state.isClosed()) {
        Log.i("Logged out...");
    }
}

My code is working fine except the part that where i am printing the username. 我的代码工作正常,除了我正在打印用户名的部分。 It is showing null in the logs. 它在日志中显示为空。 Any idea why this is happening? 知道为什么会这样吗? I have provided the necessary permissions. 我已经提供了必要的权限。

The field username is no longer existing in the Graph API v2.0, so you can't use it. Graph API v2.0中不再存在字段username ,因此您不能使用它。

See 看到

/me/username is no longer available /me/username不再可用

but there's a trick you can use. 但是您可以使用一个技巧。 After receiving the id you can make a call like this https://graph.facebook.com/ {user_id} you will get the metadata of the user containing username. 收到ID后,您可以拨打https://graph.facebook.com/ {user_id}这样的电话,您将获得包含用户名的用户的元数据。

PS To test it pick some id and check it in the browser. PS要测试它,请选择一些ID并在浏览器中检查它。 Hope it helps. 希望能帮助到你。

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

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