对于iPhone应用程序Facebook的访问令牌服务器端验证

问题描述:

我正在开发iPhone应用程序,这是基于服务器的通信,我想使用Facebook的身份验证机制。

I'm developing iPhone application, that is based on communication with server, and I want to use Facebook authentication mechanisms.

基本上,我认为它应该像这样工作:

Basically, I think it should work like this:


  1. 在我的iPhone应用程序,在Facebook的用户登录,使用他的电子邮件和密码。

  2. 用户可以访问他的相关Facebook应用程序的数据。

  3. 我的iPhone应用程序接收访问令牌,成功登录后。

  4. 在进一步沟通与我的服务器,我的iPhone应用程序应该使用Facebook的接受访问令牌(例如:在查询中)。

  5. 当我的服务器收到来自iPhone应用程序的一些查询,访问令牌,它应该问的Facebook,这个标记是有效的(和谁),如果是,服务器应该假设用户进行身份验证与Facebook。

我的问题是:服务器应该怎么问的Facebook如果给访问令牌是有效的?我想我应该以某种方式检查令牌的有效期为我的Facebook应用程序。

My question is: how the server should ask Facebook if given access token is valid? I think I should somehow check if the token is valid for my Facebook app.

我试过很多Facebook的查询图形API,我发现,但没有当过我的预期。你能提供我一些例子吗?

I've tried many Facebook queries to graph API, that I've found, but nothing worked as I expected. Can you provide me some example?

我假设你已经在手的访问令牌。在这种情况下,以验证访问令牌的最简单方法是发出以下请求

I assume that you already have the access token in hand. In such a case the simplest way to validate an access token is to issue the following request

https://graph.facebook.com/me?fields=id&access_token=@accesstoken

下面你所拥有的访问令牌替换@accesstoken。我会崩溃的网址,并解释每一个。

Here replace @accesstoken with the access token you have. I will breakdown the url and will explain each.

我们在这里发出一个图形API请求,将返回访问令牌作为一个JSON字符串的主人Facebook的用户ID。关键字我重新presents用户或访问令牌的所有者当前登录。对于这一请求访问令牌是一个强制性的参数。

We are issuing a graph api request here which will return the Facebook User Id of the owner of the access token as a JSON string. The keyword 'me' represents the currently logged in user or the owner of the access token. For this request access token is a mandatory parameter.

如果所提供的访问令牌无效或过期Facebook将只返回某种类型的错误消息。

If the provided access token is not valid or expired Facebook will just return an error message of some sort.

有关有效的访问令牌,结果会以某种方式看起来像这样

For a valid access token the result will somehow look like this

{
   "id": "ID_VALUE"
}