使用 Android 对我的服务器进行 Facebook 身份验证

发布于 2024-12-09 19:46:24 字数 479 浏览 0 评论 0原文

我正在构建一个具有以下块的应用程序:

Android - 客户端, Java Servlet - 服务器端, Facebook 应用程序 - 用于验证用户身份并处理他们的数据。

我的问题如下: 我想通过 facebook 验证我的用户(例如使用 facebook-android-sdk 从 android 客户端发送到 facebook 的请求),但随后我想将请求发送到我的服务器(由 servlet 实现)并以某种方式进行验证发送请求的用户已通过 Facebook 和我的应用程序的身份验证。

这些是步骤:

用户 X 使用 facebook-android-sdk 向 facebook 和我的 facebook 应用程序进行身份验证。 X 正在向我的服务器发送请求

至于服务器,我只想知道它是与我一起工作的正确用户,我不需要服务器执行任何图形 API 请求。

我如何知道 X 在我的服务器中有效?在这种情况下,身份验证是在客户端执行的。

I'm building an application with the following blocks:

Android - Client Side,
Java Servlets - Sever Side,
Facebook app - used in order to authenicate users and work with their data.

My Problem is the following:
I would like to authenticate my users via facebook (such as a request sent from the android client using facebook-android-sdk to facebook) but then I would like to send requests to my server (which is implemented by servlets) and to validate somehow that the user sending the request is authenticated to facebook and my application.

So these are the steps:

user X is authenicated to facebook and my facebook app using facebook-android-sdk.
X is sending a request to my server

As for the server, I would only like to know it's a proper user which is working with me, I don't need the server to perform any Graph API requests.

How can I know that X is valid in my server? The authentication was performed on client side in that case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-12-16 19:46:24

所以你有:Facebook - Android 应用程序 - 你的网络服务器。并且您的网络服务器需要知道您是您所呈现的 Facebook 用户。问题是您不能信任 Android 客户端提供给您的任何数据。

我解决了这样的问题:

  1. 从 Android 应用程序向 Facebook 验证用户身份,
  2. 将 FB 身份验证令牌获取到 Android 应用程序,
  3. 转发身份验证令牌并发送到 Facebook。 facebook UID 从 Android 到 Web 服务器,
  4. 使用 Facebook Graph 调试端点验证令牌(使用 app_id 和 user_id),如此处所述(https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#checktoken)以确保令牌用于正确的应用程序
  5. 在网络服务器上,使使用提交的令牌调用 Facebook API。

如果来自您的 Web 服务器的调试端点 API 调用返回有效信息(应用程序 ID 和用户 ID),则您的服务器可以信任该 ID(并且您可以确定 Android 身份验证是真实的)

So you have: Facebook - Android Application - Your web server. And your web server needs to know that you are the Facebook user you are presenting. The problem is that you can not trust the Android client for any data it gives to you.

I solved the problem like this:

  1. Authenticate user to Facebook from Android application,
  2. Get the FB auth token to the android app,
  3. Forward the authentication token & facebook UID from Android to web server,
  4. Verify the token (using app_id & user_id) by using Facebook Graph debug endpoint as described here (https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#checktoken) to be sure that the token is for correct application
  5. On web server, make Facebook API call with the submitted token.

If the debug endpoint API call from your web server returns valid information (app id & user id), your server can trust the id (& you can be sure that the Android authentication is real)

眉黛浅 2024-12-16 19:46:24

这个问题的更好答案(结合 tomas.tunkl 评论中的信息)如下:

  1. 从 Android 应用程序进行身份验证
  2. 从应用程序中的身份验证获取 FB Auth 令牌
  3. 将令牌和 fb UID 转发到 Web 服务器
  4. 调用调试端点,如下所述( https://developers.facebook.com/docs/facebook-login/access-tokens/debugging-and-error-handling) - 这将为您提供应用程序的应用程序 ID生成了令牌。确保应用程序 ID 是您的应用程序 ID(意味着它是来自您的移动应用程序的令牌),否则有人会使用来自另一个应用程序的令牌劫持您的应用程序,并且您正在泄漏用户数据(正如 thomas tunkl 所指出的 url https://developers.facebook.com/docs/facebook-login/security/#tokenhijacking)。这很糟糕。另请检查 is_valid/expires_at 以确保它仍然是您应用程序中的有效令牌。

(由于我链接了文档,因此我还将在此处放置来自调试和错误处理链接的一些信息,以展示如何进行调用以及返回什么:)

使用访问令牌时,您可能需要检查与其关联的信息,例如其用户或过期时间。要获取此信息,您可以使用我们的调试工具,也可以使用 API 端点。

要使用 API,您可以发出 Graph API 请求:

获取 /debug_token?
input_token={输入令牌}&
access_token={访问令牌}

input_token:您想要获取相关信息的访问令牌

access_token:您的应用程序访问令牌或来自应用程序开发人员的有效用户访问令牌
API 调用的响应是一个包含字段映射的 JSON 数组。例如:

<代码>{
“数据”: {
“app_id”:000000000000000,
"application": "社交咖啡馆",
“过期时间”:1352419328,
“is_valid”:正确,
“发布于”:1347235328,
“范围”:[
“电子邮件”,
“发布动作”
],
“用户 ID”:1207059
}
}

请注意,对于短期访问令牌,不会返回 Issued_at 字段。

这将确保您拥有 Facebook 用户的有效令牌,该令牌是根据您自己的用户密钥生成的;这意味着他们已正确进行身份验证。

The better answer to this question (incorporating info from tomas.tunkl's comment) is as follows:

  1. Authenticate from Android Application
  2. Get the FB Auth token from the authentication in app
  3. Forward the token and fb UID to web server
  4. Call the debugging endpoint as described here (https://developers.facebook.com/docs/facebook-login/access-tokens/debugging-and-error-handling) -- this will give you the app id of the app which generated the token. MAKE SURE that the app id is YOUR APP ID (meaning it is a token from your mobile app), otherwise someone is hijacking your application using a token from another application and you are leaking user data (as thomas tunkl pointed at with url https://developers.facebook.com/docs/facebook-login/security/#tokenhijacking). That is BAD. Also check the is_valid/expires_at to make sure that it is still a valid token from your app.

(Since I linked the documentation, I'm also going to put a bit of info from that debugging-and-error-handling link in here to show how to make the call and what you get back:)

When working with an access token, you may need to check what information is associated with it, such as its user or expiry. To get this information you can use our debug tool, or you can use the API endpoint.

To use the API, you can issue a Graph API request:

GET /debug_token?
input_token={input-token}&
access_token={access-token}

input_token: the access token you want to get information about

access_token: your app access token or a valid user access token from a developer of the app
The response of the API call is a JSON array containing a map of fields. For example:

{
"data": {
"app_id": 000000000000000,
"application": "Social Cafe",
"expires_at": 1352419328,
"is_valid": true,
"issued_at": 1347235328,
"scopes": [
"email",
"publish_actions"
],
"user_id": 1207059
}
}

Note that the issued_at field is not returned for short-lived access tokens.

This will ensure that you have a valid token for a facebook user that has been generated from your own secret key for a user; meaning that they have authenticated properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文