我正在尝试使用java和facebook4j facebook api包装器从facebook检索用户数据。
这是用于在从facebook接收代码后检索用户的类:
public static User getFacebookUser(final String code) {
String access_token = "";
String authURL = "https://graph.facebook.com/oauth/access_token?" +
"client_id=" + APP_ID + "&" +
"redirect_uri=" + "http://localhost:8080/online-screen/login" + "&" +
"client_secret=" + APP_SECRET_KEY + "&" +
"code=" + code;
URL url;
String result = null;
try {
url = new URL(authURL);
result = readURL(url);
} catch (IOException e) {
e.printStackTrace();
}
String[] pairs;
if (result != null) {
pairs = result.split("&");
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv[0].equals("access_token")) {
access_token = kv[1];
}
}
}
Facebook facebook = new FacebookFactory().getInstance();
facebook.setOAuthAppId(APP_ID, APP_SECRET_KEY);
AccessToken at = new AccessToken(access_token);
facebook.setOAuthAccessToken(at);
User currentUser = null;
try {
currentUser = facebook.users().getMe();
} catch (FacebookException e) {
e.printStackTrace();
}
return currentUser;
}
它可以工作,但它只接收userID和Name,所有字段都是NULL。我需要LastName,FirstName和Email。 我究竟做错了什么?请帮忙。提前完成。