使用Facebook android SDK访问Facebook用户名
我正在使用facebook 3.15 android SDK。我使用以下代码获取登录用户的用户名和其他详细信息:使用Facebook android SDK访问Facebook用户名,android,facebook,facebook-graph-api,facebook-android-sdk,Android,Facebook,Facebook Graph Api,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) {
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...");
}
}
我的代码运行良好,除了打印用户名的部分。它在日志中显示为null。知道为什么会这样吗?我已经提供了必要的权限。Graph API v2.0中不再存在
用户名
字段,因此您无法使用它
看
/me/username
不再可用
但是有一个技巧你可以使用。收到身份证后,你可以这样打电话 {user_id} 您将获得包含用户名的用户元数据 附言。
要测试它,请选择一些id并在浏览器中进行检查。希望能有所帮助。在获取令牌和用户ID后,我遇到了类似于以下内容的OAuth错误:{“错误”:{“消息”:“请求此资源需要访问令牌”。,“类型”:“OAutheException”,“code”:104,“fbtrace_ID”:“A6jAr3L3eeh”}