Java示例如何使用OAuth在GAE上使用Facebook帐户login
我search了很多,阅读了很多博客,文章,教程,但是到现在还没有得到一个可以用脸书帐号login我的应用程序的工作例子。
我知道我必须使用OAuth,获取令牌,自动化等。
任何人都可以分享一个例子 (在GAE / JAVA上)
以下是我在App Engine上的操作:
步骤1)在Facebook上注册一个“应用程序”(参见https://developers.facebook.com/ )。 你给Facebook的应用程序和URL的名称。 您注册的url是您要处理login的网页(jsp或servlet)的url。 从注册你得到两个string,一个“应用程序ID”和“应用程序的秘密”(后者是你的密码,不要把它写出来或写在HTML)。
对于这个例子,假设我注册的url是“ http://myappengineappid.appspot.com/signin_fb.do ”。
2)从一个网页,用一个button说,你将用户redirect到Facebook上的下面的URL,用你的应用程序IDreplace下面的例子中的“myfacebookappid”。 你也必须select你想要的用户的权限(参见https://developers.facebook.com/docs/reference/api/permissions/ )。 在示例中,我只要求访问用户的电子邮件。
(一个有用的事情要知道的是,你也可以传递一个可选的string,它会在“state”参数中被返回,例如,我传递用户的数据存储键,所以当Facebook传回密钥时,我可以检索用户对我来说,我不这样做)
这是一个jsp代码片段:
<%@page import="java.net.URLEncoder" %> <% String fbURL = "http://www.facebook.com/dialog/oauth?client_id=myfacebookappid&redirect_uri=" + URLEncoder.encode("http://myappengineappid.appspot.com/signin_fb.do") + "&scope=email"; %> <a href="<%= fbURL %>"><img src="/img/facebook.png" border="0" /></a>
3)你的用户将被转发到Facebook,并要求批准你要求的权限。 然后,用户将被redirect回您已注册的url。 在这个例子中,这是“ 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.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 } ...
我已经删除了error handling代码,因为您可能希望以与我不同的方式处理它。 (此外,“性别”当然是我定义的一个类。)在这一点上,您可以使用任何您想要的数据,如注册新用户或查找现有用户login。请注意, myfacebookappsecret“string当然应该是来自Facebook的应用程序秘密。
你需要使用“org.json”包来使用这个代码,你可以在http://json.org/java/find这个代码(只要带上;.java文件并将它们添加到org / json文件夹中的代码中结构体)。
我希望这有帮助。 如果有什么不清楚的地方,请做评论,我会更新答案。
Ex animo, – 亚历山大。
**** ****更新
我想添加一些信息,如果有些过分,我很抱歉。
要能够通过他/她的Facebook帐户login用户,您需要知道我们正在讨论的数据存储中的哪个用户。 如果它是一个新用户,很容易,创build一个新的用户对象(有一个名为“facebookId”的字段,或者任何你想要调用它的字段,你从Facebook获得的值),将其保存在数据存储中并login用户。
如果用户存在,则需要使用facebookId来指定该字段。 当用户从Facebookredirect时,您可以获取facebookId,然后查看数据存储以查找要login的用户。
如果你已经有用户,你需要让他们以你平常的方式login,所以你知道他们是谁,然后把他们发送到Facebook,获取facebookId并更新他们的用户对象。 这样,他们可以在下次使用Facebooklogin。
另一个小提示:用户将会在Facebook上看到一个屏幕,询问是否允许你的应用程序访问你要求的任何范围,这是没有办法的(尽pipe你要求的范围越小,似乎就越less)。 但是,这只会在用户第一次被redirect时发生(除非您稍后再询问更多示波器,否则将再次询问)。
你可以尝试face4j https://github.com/nischal/face4j/wiki 。 我们已经在我们的产品http://grabinbox.com上使用它,并且已经开放源码供任何人使用。; 它在GAE上运行良好。
在wiki上有一个例子,它可以帮助你在几分钟内整合loginfacebook。
face4j使用oAuth 2.0和facebookgraphicsAPI。
尝试实施自己的OAuth签署时遇到了很多困难。 我花了很多时间试图debugging我的令牌没有真正获得授权的问题 – 显然是一个常见的问题。 不幸的是,没有任何解决scheme为我工作,所以我最终只使用了Scribe ,一个漂亮的Java OAuth库,除了支持Facebook以外的其他提供者(例如Google,Twitter等)
您可以看看LeanEngine,服务器部分: https : //github.com/leanengine/LeanEngine-Server/tree/master/lean-server-lib/src/main/java/com/leanengine/server/auth
检查Facebook的Java API。
其他示例: http : //code.google.com/p/facebook-java-api/wiki/Examples
- Google App Engine帐户可以使用多lessMemcache内存?
- Google云端点限制…任何build议的解决scheme?
- 带有InputStream长度示例的AmazonS3 putObject
- 根据文件夹结构,Google App Engine应用中的默认服务/模块可以是非默认服务/模块的兄弟吗?
- 频道API存在不触发
- 您如何从(非Web)Python客户端访问经过身份validation的Google App Engine服务?
- Google Blobstore与Google云端存储
- 将字节数组转换为string(Java)
- 在开发服务器上configurationAppIdentityService的使用