0
我試圖使用fql_query獲取登錄的名字。facebook-java-api:fql_query返回null
我嘗試以下代碼:
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(最新版本)
- 爲什麼我從查詢中得到一個空文檔?
- 爲什麼查詢在出現問題時不會拋出任何相關異常?
- 這是我在網上找到的代碼,有沒有更好的方法來實現我的目標?
謝謝你的一切!
我在這個學科真的很新,所以我對任何不相關的問題或主題的信息表示歉意。
也沒找到答案了,要去嘗試,而不是Java API的客戶端TinyFbClient。也許我會有更好的運氣 – ufk 2010-02-02 13:57:28