4

我正在编写一个使用 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?

感谢您的时间

4

1 回答 1

0
  1. 您遇到了关于 OAuth 的基本误解之一:OAuth 就是关于授权的。用户授予客户)代表访问数据的权限。如果您想对用户进行身份验证,一些提供商允许您访问一个唯一 ID,以便您区分用户。所以,是的,如果你想识别一个用户,你必须请求他的唯一 id

  2. 有多种方法可以做到这一点。所有这些都是关于在重定向用户之前生成您存储的唯一会话 ID 。一旦用户回来,他会返回一个id给你。如果您看到您发布并存储了该id,则删除该id并且您知道您正在与哪个用户交谈。您可以将此id存储在 cookie 中,也可以将其作为参数的一部分传递。state

  3. 重定向之前的任何时间都是可能的。如果您想随时知道正在与哪个用户交谈,只需在他第一次呼叫您时执行此操作即可。

于 2012-11-03T03:49:24.970 回答