我正在编写一个使用 O-Auth 的“与 Facebook 连接”的移动应用程序,以便:
1) Let Facebook deal with the authentication
2) Not force my users to register to another website
3) Get Facebook information
我已经实现了 Facebook 的服务器端登录(在 JAVA 中):
https://developers.facebook.com/docs/howtos/login/server-side-login/
我不明白一旦用户连接到 facebook,我该如何维护用户会话。
这是从端到端的流程,如果我错了,请纠正我。
1) The user logs into my server (Android APP using WebView)
2) The server redirect the user to Facebook (with client_id, redirect_url, state and permissions)
3) The user is now facing Facebook's login dialog.
3.1) If the user deny: Facebook calls the redirect_url and notify the server that there was an error. (Flow Ends here)
3.2) If the user accept: Facebook calls the redirect_url (my server) with the state i've set (for CSRF protection) and a code.
4) The server contacts Facebook and send it the app_id, redirect_url, client_secret and code and in return, facebook calls the redirect_url with a valid token. This token allows the server to issue facebook API calls on behalf of the user.
到目前为止,一切都很好。现在到主要问题 - 会话管理。
现在我得到了令牌,我需要弄清楚它属于哪个用户,这样我就可以从我的数据库中获取数据或创建一个新条目(如果是新用户)。
1)我应该使用什么样的标识符来识别我的服务器数据库中的用户?(我是否需要调用 Facebook API 来检索基本信息并从中提取电子邮件和/或唯一 ID?
2) 获得此唯一 ID 后,如何再次联系用户?在第 2 步,服务器将他重定向到 Facebook,这意味着他打开了与 Facebook 的新连接,并且他不再连接到服务器。现在他走了,我怎么给他做饼干?<-- *这部分对我来说是最令人困惑的。*
3)什么时候我需要在用户的请求上设置一个会话 ID(cookie),以便下次他联系我时我会知道它是谁。
我认为堆栈溢出是说明我的问题的最佳示例。我点击了“与 facebook 连接”按钮,突然间我成为了 Stack Overflow 的用户,没有任何注册。堆栈溢出如何知道如何提取我已经提出的问题?它是如何识别我的?在我描述的链条中的哪个点上,它在我的请求中设置了一个 cookie?
感谢您的时间