使用 Facebook 进行身份验证的网站的 REST API [英] REST API for website which uses Facebook for authentication

查看:29
本文介绍了使用 Facebook 进行身份验证的网站的 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个网站,唯一登录和验证网站的方式是使用 Facebook(这不是我的选择).首次登录 Facebook 时,系统会自动为您创建一个帐户.

We have a website where the only way to login and authenticate yourself with the site is with Facebook (this was not my choice). The first time you login with Facebook, an account gets automatically created for you.

我们现在想要为我们的网站创建一个 iPhone 应用程序,并为其他人使用我们的服务创建一个公共 API.

We now want to create an iPhone application for our site and also a public API for others to use our service.

这个问题是关于如何从应用程序/API 对我们的网站进行身份验证,分为两部分:

This question is about how to authenticate with our website from the app/API and is broken into 2 parts:

  1. 处理从 API 到仅使用 Facebook OAuth 作为身份验证方法的网站的 REST 身份验证的正确方法是什么?

我已经阅读并研究了很多关于 REST API 的标准身份验证方法.我们不能使用基于HTTPS的基本身份验证之类的方法,因为没有凭据用户等.像这个之类的东西似乎是仅用于验证使用 API 的应用程序.

I have read and researched a lot about standard methods of authentication for REST API. We can't use such methods as Basic Auth over HTTPS, as there are no credentials for a user as such. Something like this seems to be only for authenticating applications using the API.

目前,我能想到的最好方法是您在我们的 API 上点击/authorize 端点,它重定向到 Facebook OAuth,然后重定向回站点并提供 API 用户可以使用的令牌"验证后续请求.

Currently, the best way I can think is you hit an /authorize end-point on our API, it redirects to Facebook OAuth, then redirects back to the site and provides a 'token' which the user of the API can use to authenticate subsequent requests.

  1. 对于我们创建的官方应用程序,我们不一定需要以相同的方式使用公共 API.那么,与我们的网站对话并验证用户身份的最佳方式是什么?

我了解(我认为)如何使用 API(公共)密钥和秘密(私有)密钥对使用我们 API 的 3rd 方应用程序进行身份验证.但是,在验证使用该应用程序的用户时,当我们必须验证用户身份的唯一方法是 Facebook 时,我对如何进行操作感到相当困惑.

I understand (I think) how to authenticate 3rd-party applications that are using our API, using API (public) keys and secret (private) keys. However, when it comes to authenticating the user who is using the app, I am getting rather confused about how to go about it when the only way we have to authenticate a user is Facebook.

我觉得我遗漏了一些非常明显的东西,或者没有完全理解公共 REST API 应该如何工作,因此我们将不胜感激任何建议和帮助.

I feel like I'm missing something very obvious, or don't fully understand how public REST APIs should work, so any advice and help would be greatly appreciated.

推荐答案

UPDATE:见下文

我也一直在认真思考这个问题.对我来说还不是很清楚,但这是我正在考虑的路线.我正在创建一个 REST API,我的用户通过 Facebook 连接进行身份验证.

UPDATE: see below

I've been thinking hard about this question too. It's not entirely clear to me yet but here's the route I am thinking of going. I am creating a REST API an my users only auth with Facebook connect.

在客户端:

  1. 使用 Facebook API 登录并获取 OAUTH2 代码.
  2. 将此代码交换为访问令牌.
  3. 在每次调用我的自定义 API 时,我都会包含 Facebook 用户 ID 和访问令牌.

在 API 上(对于每个需要用户身份验证的方法):

On the API (for every method that requires user authentication):

  1. 使用上面的访问令牌向/me Facebook 图发出请求.
  2. 验证返回的 Facebook 用户 ID 与从上面传递给我的 API 的用户 ID 匹配.
  3. 如果访问令牌已过期,则需要额外的通信.

我还没有对此进行测试.听起来怎么样?

I have yet to test this. How does it sound?

我只在登录时使用上述交换一次.一旦我确定了哪个用户正在登录,我就会创建自己的访问令牌,并且从那时起就使用该令牌.所以新的流程看起来像这样...

I only use the above exchange once upon login. Once I determine which user is logging in, I create my own access token, and that token is used from that point going forward. So the new flow looks like this...

在客户端:

  1. 使用 Facebook API 登录并获取 OAUTH2 代码.
  2. 将此代码交换为访问令牌.
  3. 我的 API 请求访问令牌,包括 Facebook 令牌作为参数
  1. Use the Facebook API to login and get an OAUTH2 code.
  2. Exchange this code for an access token.
  3. Request an access token from my API, including the Facebook token as a parameter

关于 API

  1. 接收访问令牌请求.
  2. 使用 Facebook 访问令牌向/me Facebook 图发出请求
  3. 验证 Facebook 用户是否存在并与我的数据库中的用户匹配
  4. 创建我自己的访问令牌,保存并返回给客户端,以便从现在开始使用

这篇关于使用 Facebook 进行身份验证的网站的 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆