通过访问令牌从Facebook获取数据

时间:2015-11-07 07:00:27

标签: java servlets facebook-access-token restfb 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资源管理器来查看我通过相同的访问令牌得到的性别,我得到了性别的价值,但是当我通过Servlet执行此操作时,我会得到“null”。< / p>

以下是我的代码: -

package com.bitspedia.servlets;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.exception.FacebookException;
import com.restfb.types.User;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class checking
 */
@WebServlet("/FriendsListServlet")

public class FriendsListServlet extends HttpServlet {
    public static String APP_ID = "";
    public static String APP_SECRET = "";
    private static final long serialVersionUID = 1L;

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

        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();
        request.setAttribute("friendsList",friendsList);

        getServletConfig().getServletContext().getRequestDispatcher("/FriendsList.jsp").forward(request, response);
    }

    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }
}

请帮忙: - (

0 个答案:

没有答案