简体   繁体   中英

How to add Facebook friends upcoming birthdays to my app?

I'm working on a calendar app, and I wanted to fetch upcoming birthdays from Facebook, so I read about it and found that i need to create App using Facebook's Graph API explorer, I did, but it didn't work for my, when I try to get my friends list I only get an empty list, Here's the code I used, there's no errors, but the list is empty.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

    String URLEncodedRedirectURI = URLEncoder.encode("http://localhost:8084/facebookfriends/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);

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

    List<User> friendsList = friends.getData();
    int count = friendsList.size();
    request.setAttribute("friendsList", friendsList);
    request.getRequestDispatcher("FriendsList.jsp").forward(request, response);
}

You cannot get the friends birthdays anymore without them using your app. The friends_* permissions have been removed with the introduction of the Graph API.

See

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM