繁体   English   中英

使用Facebook android SDK访问Facebook用户名

[英]Accessing Facebook username using Facebook android SDK

我正在使用facebook 3.15 android SDK。 我正在使用以下代码来获取登录用户的用户名和其他详细信息:

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...");
    }
}

我的代码工作正常,除了我正在打印用户名的部分。 它在日志中显示为空。 知道为什么会这样吗? 我已经提供了必要的权限。

Graph API v2.0中不再存在字段username ,因此您不能使用它。

看到

/me/username不再可用

但是您可以使用一个技巧。 收到ID后,您可以拨打https://graph.facebook.com/ {user_id}这样的电话,您将获得包含用户名的用户的元数据。

PS要测试它,请选择一些ID并在浏览器中检查它。 希望能帮助到你。

暂无
暂无

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

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