facebook-java-api:fql_query 返回 null

发布于 2024-08-20 05:30:59 字数 3996 浏览 6 评论 0原文

我正在尝试使用 fql_query 获取登录的名字。

我尝试了以下代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.*;
import javax.servlet.*;
import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookWebappHelper;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.IFacebookRestClient;
import org.json.JSONException;


public class Canvas extends HttpServlet {

     private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";
    private String api_key=<API_KEY>;
    private String secret=<SECRET>;


  @Override
    public void doGet (HttpServletRequest req,
                                     HttpServletResponse res)
    throws ServletException, IOException
  {
                    HttpServletRequest request = (HttpServletRequest)req;
                    HttpServletResponse response = (HttpServletResponse)res;

                    HttpSession session = request.getSession(true);
                    IFacebookRestClient<Document> userClient = getUserClient(session);
                    if(userClient == null) {
                      //  logger.debug("User session doesn't have a Facebook API client setup yet. Creating one and storing it in the user's session.");
                        userClient = new FacebookXmlRestClient(api_key, secret);
                        session.setAttribute(FACEBOOK_USER_CLIENT, userClient);
                    }
 FacebookWebappHelper<Document> facebook = new FacebookWebappHelper<Document>(request,      response, api_key, secret, userClient);
                    String nextPage = request.getRequestURI();
                    nextPage = nextPage.substring(nextPage.indexOf("/", 1) + 1); //cut out the first /, the context path and the 2nd /
            //      logger.trace(nextPage);
                    boolean redirectOccurred = facebook.requireLogin(nextPage);
                    if(redirectOccurred) {
                            return;
                    }
                    redirectOccurred = facebook.requireFrame(nextPage);
                    if(redirectOccurred) {
                            return;
                    }

                    long facebookUserID;
                    try {
                         facebookUserID = userClient.users_getLoggedInUser();
                    } catch(FacebookException ex) {
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,  "Error while fetching user's facebook ID");
/*                          logger.error("Error while getting cached (supplied by request params) value " +
                                         "of the user's facebook ID or while fetching it from the Facebook service " +
                                         "if the cached value was not present for some reason. Cached value = {}", userClient.getCacheUserId());*/
                        return;
                    }
                      PrintWriter out = res.getWriter();
        String query = "Select name from user where uid=" + facebookUserID;
        Document fqlDocument=null;
        try {
        fqlDocument = userClient.fql_query(query);
        } catch (FacebookException e) {
            out.println("error");
        }
    out.println("Hello, Brave new World: " + facebookUserID + " " +  fqlDocument);
    out.close();



  }
  public static FacebookXmlRestClient getUserClient(HttpSession session) {
        return (FacebookXmlRestClient)session.getAttribute(FACEBOOK_USER_CLIENT);
    }

}

结果输出是:

Hello, Brave new World: <MY_UID>[#document: null]

我确信我收到的 uid 是正确的。 使用 facebook-java-api 3.0.2(最新版本)

  1. 为什么我从查询中得到一个空文档?
  2. 如果出现问题,为什么查询没有抛出任何相关异常?
  3. 这是我在网上找到的代码,有没有更好的方法来实现我的目标?

感谢一切!

我对这个主题非常陌生,因此对于任何与该主题不相关的问题或信息,我深表歉意。

I'm trying to fetch logged-in first name using fql_query.

i tried the following code:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.*;
import javax.servlet.*;
import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookWebappHelper;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.IFacebookRestClient;
import org.json.JSONException;


public class Canvas extends HttpServlet {

     private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";
    private String api_key=<API_KEY>;
    private String secret=<SECRET>;


  @Override
    public void doGet (HttpServletRequest req,
                                     HttpServletResponse res)
    throws ServletException, IOException
  {
                    HttpServletRequest request = (HttpServletRequest)req;
                    HttpServletResponse response = (HttpServletResponse)res;

                    HttpSession session = request.getSession(true);
                    IFacebookRestClient<Document> userClient = getUserClient(session);
                    if(userClient == null) {
                      //  logger.debug("User session doesn't have a Facebook API client setup yet. Creating one and storing it in the user's session.");
                        userClient = new FacebookXmlRestClient(api_key, secret);
                        session.setAttribute(FACEBOOK_USER_CLIENT, userClient);
                    }
 FacebookWebappHelper<Document> facebook = new FacebookWebappHelper<Document>(request,      response, api_key, secret, userClient);
                    String nextPage = request.getRequestURI();
                    nextPage = nextPage.substring(nextPage.indexOf("/", 1) + 1); //cut out the first /, the context path and the 2nd /
            //      logger.trace(nextPage);
                    boolean redirectOccurred = facebook.requireLogin(nextPage);
                    if(redirectOccurred) {
                            return;
                    }
                    redirectOccurred = facebook.requireFrame(nextPage);
                    if(redirectOccurred) {
                            return;
                    }

                    long facebookUserID;
                    try {
                         facebookUserID = userClient.users_getLoggedInUser();
                    } catch(FacebookException ex) {
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,  "Error while fetching user's facebook ID");
/*                          logger.error("Error while getting cached (supplied by request params) value " +
                                         "of the user's facebook ID or while fetching it from the Facebook service " +
                                         "if the cached value was not present for some reason. Cached value = {}", userClient.getCacheUserId());*/
                        return;
                    }
                      PrintWriter out = res.getWriter();
        String query = "Select name from user where uid=" + facebookUserID;
        Document fqlDocument=null;
        try {
        fqlDocument = userClient.fql_query(query);
        } catch (FacebookException e) {
            out.println("error");
        }
    out.println("Hello, Brave new World: " + facebookUserID + " " +  fqlDocument);
    out.close();



  }
  public static FacebookXmlRestClient getUserClient(HttpSession session) {
        return (FacebookXmlRestClient)session.getAttribute(FACEBOOK_USER_CLIENT);
    }

}

the resulted output is:

Hello, Brave new World: <MY_UID>[#document: null]

i reassured that the uid that i received is the correct one.
using facebook-java-api 3.0.2 (latest version)

  1. why did i get an empty document from the query ?
  2. why did the query not throw any relevant exception if there was a problem?
  3. this is a code that i found on the net, is there any better method to implement my goals ?

thanks for everything!

i'm really new in this subject so i apologize for any un-relevant questions or information on the subject.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-08-27 05:30:59

我通过使用 TinyFBClient 而不是 facebook-java-api 解决了该问题。

tinyFbClient 允许发送实际的 facebook api 命令,这使我无需使用 fql 查询即可获取应用程序的所有相关信息。

facebook API: http://wiki.developers.facebook.com/index.php/API

tinyFBClient:http://www.socialjava.com/

代码示例:

HttpServletRequest request = (HttpServletRequest)req;
String sessionKey = request.getParameter("fb_sig_session_key");
TinyFBClient fb = new TinyFBClient(this.api_key,this.secret,sessionKey);
TreeMap tm = new TreeMap();

tm.put("uid","");

String friendList = fb.call("friends.get", tm);

PrintWriter out = res.getWriter();
out.println(friendList);

out.close();

}

I resolved the issue by using TinyFBClient instead of facebook-java-api.

tinyFbClient allows to send the actual facebook api commands which allows me to get a all the relevant information for my application without using fql queries at all.

facebook api: http://wiki.developers.facebook.com/index.php/API

tinyFBClient: http://www.socialjava.com/

code sample:

HttpServletRequest request = (HttpServletRequest)req;
String sessionKey = request.getParameter("fb_sig_session_key");
TinyFBClient fb = new TinyFBClient(this.api_key,this.secret,sessionKey);
TreeMap tm = new TreeMap();

tm.put("uid","");

String friendList = fb.call("friends.get", tm);

PrintWriter out = res.getWriter();
out.println(friendList);

out.close();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文