无法从java servlet中的facebook访问令牌获取所有数据

时间:2015-11-02 11:17:22

标签: java facebook-graph-api facebook-access-token facebook-graph-api-v2.4

我正在尝试获取fb用户的“朋友列表”和他/她的“关于”,但是当我这样做时,我会得到某些字段的空值,就像我在下面评论一样。

System.out.println(loginUser.getId());  //show id
System.out.println(loginUser.getName());  //Show Name
System.out.println(loginUser.getFirstName());  //show null
 System.out.println(loginUser.getGender());  //show null 
System.out.println(loginUser.getAbout());  //show null

我一直在尝试使用graph-api资源管理器来查看我的性别问题 https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html#keytool_option_list

代码:

String code = request.getParameter("code");

String URLEncodedRedirectURI = URLEncoder.encode("http://localhost:8080/bitspedia-fetchfbfriends/FriendsListServlet");
String MY_ACCESS_TOKEN = "";

String authURL = "https://graph.facebook.com/oauth/access_token?" +
        "client_id=" + FriendsListServlet.APP_ID + "&" +
        "redirect_uri=" + URLEncodedRedirectURI + "&" +
        "client_secret=" + FriendsListServlet.APP_SECRET + "&" +
        "code=" + code;

URL url = new URL(authURL);

String result = readURL(url);
String[] pairs = result.split("&");

for (String pair : pairs) {

    String[] kv = pair.split("=");
    if (kv[0].equals("access_token")) {
        MY_ACCESS_TOKEN = kv[1];
    }
}
FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN, FriendsListServlet.APP_SECRET);
Connection<User> friends = null;

try {
    User loginUser = facebookClient.fetchObject("me", User.class);
    request.setAttribute("loginUser", loginUser);
    friends = facebookClient.fetchConnection("/me/friends", User.class);
    System.out.println(loginUser.getId());  //shows id
    System.out.println(loginUser.getName());  //Shows  name
    System.out.println(loginUser.getFirstName()); //shows null
    System.out.println(loginUser.getWebsite()); //shows null
    System.out.println(loginUser.getAbout()); //shows null

} catch (FacebookException e) {
    e.printStackTrace();
}

List<User> friendsList = friends.getData();

1 个答案:

答案 0 :(得分:0)

它有一个非常小的问题,你没有提供null显示值的参数,所以做一些小改动就可以了。

{{1}}