java中的Facebook oauth身份验证仅返回用户名和ID

时间:2017-01-22 04:17:05

标签: java facebook oauth

我正在尝试获取用户详细信息,oauth通过facebook api和java验证用户,并且能够验证并重定向到我的主页,但facebook api没有返回电子邮件等详细信息,生日,朋友列表但仅返回用户名和ID ,其余详细信息为null。我已经在Facebook上注册了我的应用程序,仍然为什么我无法获取其他详细信息,如生日,电子邮件,朋友列表,个人资料图片等。

以下是代码:

这是主菜单类:

 public class MainMenu extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private String code="";

    public void service(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {      


         String clientId = "XXXXXXXXXXXXXXXXX";
         String clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        //public static final String REDIRECT_URI = "http://localhost:8080/Facebook_Login/fbhome";

        Client client = new Client(clientId, clientSecret);
        FacebookFactory facebookFactory = new FacebookFactory(client);
        String redirectURL = facebookFactory.getRedirectURL("http://localhost:8080/Facebook_Login/fbhome", Display.POPUP, Permission.EMAIL, Permission.OFFLINE_ACCESS);
        String code = req.getParameter("code");
        OAuthAccessToken accessToken1;
        try {
            accessToken1 = facebookFactory.getOAuthAccessToken(code, "http://localhost:8080/Facebook_Login/fbhome");

        Facebook facebook = facebookFactory.getInstance(accessToken1);
        User fbUser = facebook.getCurrentUser();



        System.out.println("fbUser"+"====="+fbUser.getName());

        ServletOutputStream out = res.getOutputStream();
        res.reset();
        out.println("<h1>Facebook Login using Java</h1>");
        out.println("<h2>Application Main Menu</h2>");
        out.println("<div>Welcome "+fbUser.getName());
        out.println("<div>Your Religion: "+fbUser.getReligion());
        out.println("<div>Your Birthday: "+fbUser.getBirthday());

        out.println("<div>Your Gender: "+fbUser.getGender());
        out.println("<div>Your Locale: "+fbUser.getLocale());

        out.println("<div>Your ID: "+fbUser.getWork());
        out.println("<div>Logout: "+"<a href='https://www.facebook.com/logout.php?next="+"http://localhost:8080/Facebook_Login/fbhome&access_token="+accessToken1+"'>Logout"+"</>");

        //out.println("<div>Your Education: "+fbUser.home(facebook));
        Map<String, String> fbProfile = new HashMap<String, String>();
        JSONObject json = new JSONObject(fbUser);
        try {
            fbProfile.put("id", json.getString("id"));
            System.out.println("fbProfile"+fbProfile);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }






        } catch (FacebookException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();


        }



        /*out.println("<div>You are "+fbProfileData.get("gender"));
        out.flush();*/

    }

}

我正在使用face4j-1.5.11.jar

下面是html

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Java Facebook Login</title>
</head>
<body style="text-align: center; margin: 0 auto;">
    <div
        >
        <a href="<%=fbConnection.getFBAuthUrl()%>"> <img
            style="margin-top: 138px;" src="./img/facebookloginbutton.png" />
        </a>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

默认情况下,图形API不会返回所有字段,因此我认为您使用的库不允许您请求更多字段。

您可以使用控制台https://developers.facebook.com/tools/explorer/

进行测试
$ fbapi '/v2.8/me'
{
  "name": "Yuri Schimke",
  "id": "zzzzzz"
}

$ fbapi '/v2.8/me?fields=name,email,birthday,picture&debug=all'
{
  "name": "Yuri Schimke",
  "email": "www@xxxx.zz",
  "birthday": "xx/xx/xxxx",
  "picture": {
    "data": {
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/xxxx.jpg"
    }
  },
  "id": "xxxx",
  "__debug__": {}
}