这是我在App Engine上的操作方法:
第1步)在Facebook上注册“应用”(请参阅https://developers.facebook.com/)。您为Facebook指定该应用程序的名称和一个URL。您注册的url是您要处理登录的页面(jsp或servlet)的URL。从注册中,您将获得两个字符串,一个“应用程序ID”和一个“应用程序密码”(后者是您的密码,请不要给出此密码或将其写入html)。
对于此示例,假设我注册的网址是“
http://myappengineappid.appspot.com/signin_fb.do
”。
2)从网页上说出一个按钮,您将用户重定向到Facebook上的以下URL,在以下示例中将您的应用程序ID替换为“
myfacebookappid”。您还必须选择要询问用户的权限(或“范围”)(请参阅https://developers.facebook.com/docs/reference/api/permissions/)。在示例中,我仅请求访问用户的电子邮件。
(要知道的一件有用的事情是,您还可以传递一个可选字符串,该字符串将在“
state”参数中保持不变。例如,我传递了用户的数据存储区密钥,因此我可以在Facebook传递回该密钥时检索用户对我来说。我在示例中不这样做。)
这是一个jsp片段:
<%@page import="java.net.URLEnprer" %><% String fbURL = "http://www.facebook.com/dialog/oauth?client_id=myfacebookappid&redirect_uri=" + URLEnprer.enpre("http://myappengineappid.appspot.com/signin_fb.do") + "&scope=email";%><a href="<%= fbURL %>"><img src="/img/facebook.png" border="0" /></a>3)您的用户将被转发到Facebook,并被要求批准您要求的权限。然后,用户将被重定向回您已注册的网址。在此示例中,这是“
http://myappengineappid.appspot.com/signin_fb.do
”,它在我的web.xml中映射到以下servlet:
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.URLEnprer;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 pre = req.getParameter("pre"); if (pre == null || pre.equals("")) { // an error occurred, handle this } String token = null; try { String g = "https://graph.facebook.com/oauth/access_token?client_id=myfacebookappid&redirect_uri=" + URLEnprer.enpre("http://myappengineappid.appspot.com/signin_fb.do", "UTF-8") + "&client_secret=myfacebookappsecret&pre=" + pre; 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 pre: " + pre); } 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 } ...我删除了错误处理代码,因为您可能希望与我不同地处理它。(此外,“性别”当然是我定义的类。)此时,您可以将数据用于所需的任何用途,例如注册新用户或寻找现有用户登录。请注意,“
“ myfacebookappsecret”字符串当然应该是您来自Facebook的应用程序秘密。
您将需要“ org.json”包才能使用此代码,可以在以下位置找到该代码:http :
//json.org/java/(只需获取.java文件并将其添加到org /
json文件夹中的代码中,结构体)。
我希望这有帮助。如果不清楚,请发表评论,我将更新答案。
前animo,-亚历山大。
*更新*
我想补充一些信息,如果其中一些内容似乎有点过分,我深表歉意。
为了能够通过他/她的Facebook帐户登录用户,您需要知道我们正在谈论的数据存储中的哪个用户。如果是新用户,则很容易,创建一个新的用户对象(具有一个名为“
facebookId”的字段,或者您想调用它的任何对象,其值来自Facebook),将其保留在数据存储区中并登录该用户。
如果该用户存在,则需要使该字段具有facebookId。从Facebook重定向用户后,您可以获取facebookId,然后在数据存储区中查找要登录的用户。
如果您已经有用户,则需要让他们以通常的方式登录,以便知道他们是谁,然后将其发送到Facebook,返回facebookId并更新其用户对象。这样,他们可以下次使用Facebook登录。
另一个小注意事项:在Facebook上将向用户显示一个屏幕,要求您的应用访问您想要的任何范围,但没有办法解决(想要的范围越少,看上去越不那么侵入)。但是,只有在第一次重定向用户时才会发生这种情况(除非您稍后再请求更多作用域,然后它将再次询问)。



