【问题标题】:Facebook oauth authentication in java only returns username and idjava中的Facebook oauth身份验证仅返回用户名和ID
【发布时间】:2023-04-03 02:04:01
【问题描述】:

我正在尝试通过 facebook api 和 java 获取用户详细信息和 oauth 身份验证用户,并且能够进行身份验证并重定向到我的主页,但 facebook api 没有返回诸如 电子邮件、生日、朋友列表等详细信息 但只返回 username 和 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>

【问题讨论】:

  • 所以你做错了,但因为你没有告诉我们你做了什么,我们无法帮助你
  • 现在你可以看看..的代码..请建议!

标签: java facebook oauth


【解决方案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__": {}
}

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2011-09-17
    • 2011-07-07
    • 2013-07-05
    • 2014-05-14
    • 2017-12-15
    • 2012-11-04
    • 2011-09-09
    相关资源
    最近更新 更多