使用OAuth 2.0登录Facebook

问题描述:

  1. 我希望人们使用其Facebook帐户登录到我的网站.
  2. 我需要从他们的Facebook个人资料中提取一些信息,并将其添加到我网站的数据库中

我尝试使用OAuth 2.0方法来重定向到该网址

I have tried using the OAuth 2.0 method which makes a redirect to this url

https://www.facebook.com/dialog/oauth?
    client_id=YOUR_APP_ID
   &redirect_uri=YOUR_REDIRECT_URI
   &scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES
   &state=SOME_ARBITRARY_BUT_UNIQUE_STRING

我成功验证了用户身份,但现在出现了主要问题.我如何访问作为响应发送的数据,我只能看到一个名为codeGET变量和一些长文本.如何转换为可用数据?

I successfully authenticated the user but now the main problem arose. How do I access the data which is sent as response all I can see is a GET variable named code and some long text. How do I convert to usable data?

我在自己的网站上使用php

I use php for my website

将代码交换为用户访问令牌

Exchange the code for a user access token

一旦用户授权了您的应用,您应该发出服务器端请求,以将上面返回的代码交换为用户访问令牌.

Once the user has authorized your app, you should make a server side request to exchange the code returned above for a user access token.

https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&redirect_uri=YOUR_REDIRECT_URI
&client_secret=YOUR_APP_SECRET
&code=CODE_GENERATED_BY_FACEBOOK

client_secret参数必须是应用程序设置中显示的应用程序密钥".该请求的响应主体将是以下形式的url编码字符串:

The client_secret parameter must be the App Secret as shows in your app's settings. The body of the response to this request will be a url encoded string of the form:

access_token=USER_ACESS_TOKEN&expires=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES

您应该解析此字符串,并使用access_token值向Graph API发出请求.您还应该将访问令牌保留在数据库中,以便向API提出进一步请求,而不必重新验证用户身份.

You should parse this string and use the access_token value to make requests to the Graph API. You should also persist the access token in your database in order to make further requests to the API without having to re-authenticate the user.

一旦访问令牌到期时间到,令牌将变得无效,并且不能再用于对API的请求中.要获取新的用户访问令牌,您必须再次使用户通过此流程.但是,如果用户未取消对您的应用程序的授权,并且您要求的权限不超过用户已授予您的应用程序的权限,那么将不会显示任何对话框,并且将使用新代码透明地将用户重定向到您的redirect_uri可以换成新的用户访问令牌.

Once the access token expiry time is reached, the token will become invalid and can no longer be used in requests to the API. To obtain a new user access token, you must pass the user through this flow again. However, if the user has not deauthorized your app and you're asking for no permissions beyond those which the user has already granted your application, then no dialog will be displayed and the user will be transparently redirected to your redirect_uri with a fresh code which can be exchanged for a fresh user access token.

如果在将代码交换为用户访问令牌时遇到问题,授权服务器将发出HTTP 400,并在响应的正文中以JSON对象的形式返回错误:

If there is an issue exchanging the code for a user access token, the authorization server will issue an HTTP 400 and return the error as a JSON object in the body of the response:

{
   "error": {
      "type": "OAuthException",
      "message": "Error validating verification code."
   }
}

有关更多参考,请参见 http://developers.facebook.com/docs/authentication /server-side/

For further reference checkout http://developers.facebook.com/docs/authentication/server-side/

向Graph API发出请求

Making requests to the Graph API

使用有效的用户访问令牌,您可以请求从Graph API读取和写入数据.常见的第一个请求是获取刚刚对您的应用进行身份验证的用户的基本信息(包括ID和名称):

With a valid user access token, you can make requests to read and write data from the Graph API. A common first request would be to get the basic information (including the id and name) of the user who just authenticated your app:

https://graph.facebook.com/me?access_token=YOUR_USER_ACCESS_TOKEN