我正在按照此答案中提到的步骤进行操作:https://stackoverflow.com/a/9580644/4950201
我成功注册了一个Facebook应用程序并从网页调用登录对话框(用户点击登录到Facebook,他登录,并且Facebook将他重定向到redirect_uri并附加了代码)。
现在我不知道如何在服务器端使用以下Java代码(摘自上面的答案):
import org.json.JSONObject;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SignInFB extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String code = req.getParameter("code");
if (code == null || code.equals("")) {
// an error occurred, handle this
}
String token = null;
try {
String g = "https://graph.facebook.com/oauth/access_token?client_id=myfacebookappid&redirect_uri=" + URLEncoder.encode("http://myappengineappid.appspot.com/signin_fb.do", "UTF-8") + "&client_secret=myfacebookappsecret&code=" + code;
URL u = new URL(g);
URLConnection c = u.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer b = new StringBuffer();
while ((inputLine = in.readLine()) != null)
b.append(inputLine + "\n");
in.close();
token = b.toString();
if (token.startsWith("{"))
throw new Exception("error on requesting token: " + token + " with code: " + code);
} catch (Exception e) {
// an error occurred, handle this
}
String graph = null;
try {
String g = "https://graph.facebook.com/me?" + token;
URL u = new URL(g);
URLConnection c = u.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer b = new StringBuffer();
while ((inputLine = in.readLine()) != null)
b.append(inputLine + "\n");
in.close();
graph = b.toString();
} catch (Exception e) {
// an error occurred, handle this
}
String facebookId;
String firstName;
String middleNames;
String lastName;
String email;
Gender gender;
try {
JSONObject json = new JSONObject(graph);
facebookId = json.getString("id");
firstName = json.getString("first_name");
if (json.has("middle_name"))
middleNames = json.getString("middle_name");
else
middleNames = null;
if (middleNames != null && middleNames.equals(""))
middleNames = null;
lastName = json.getString("last_name");
email = json.getString("email");
if (json.has("gender")) {
String g = json.getString("gender");
if (g.equalsIgnoreCase("female"))
gender = Gender.FEMALE;
else if (g.equalsIgnoreCase("male"))
gender = Gender.MALE;
else
gender = Gender.UNKNOWN;
} else {
gender = Gender.UNKNOWN;
}
} catch (JSONException e) {
// an error occurred, handle this
}
...
当用户登录时,Facebook会将他重定向到redirect_uri,而在redirect_uri页面,我必须从客户端调用服务器端功能服务(从脚本调用@service)?
如果是,那么我应该将哪些参数传递给服务功能? (它获取以下参数(HttpServletRequest req,HttpServletResponse res))
我理解Java代码和获取访问令牌的过程,但是在Facebook将用户重定向到redirect_uri之后,我没有任何关于如何使用和调用上述函数的线索。
如果有人可以帮助我,我会很高兴。
由于
答案 0 :(得分:0)
您不必从脚本中调用它。基本上,您的重定向URI本身应该是@RequestMapping
。在此请求映射中,您必须编写逻辑来捕获Facebook提供给您的 access_token (您看到附加到上述重定向URI的代码)并将其安全存储。
与FB的所有进一步交互应该在控制器中写为单独的@RequestMapping
。对于例如你可以让@RequestMapping说ListFriends,你可以列出你所有的朋友。
此ListFriends servlet中的逻辑将使用访问令牌(在上一步中获得)与Facebook API通信,然后将列表返回到将列表呈现到网页的层。
希望这有帮助。