简体   繁体   English

使用OAuth 2.0登录Facebook

[英]Facebook login using OAuth 2.0

  1. I want people to log in to my site with their Facebook accounts. 我希望人们使用其Facebook帐户登录到我的网站。
  2. I need to pull some info from their Facebook profile and add it to my site's database 我需要从他们的Facebook个人资料中提取一些信息,并将其添加到我的网站的数据库中

I have tried using the OAuth 2.0 method which makes a redirect to this url 我尝试使用OAuth 2.0方法,该方法将重定向到此网址

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

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. 我如何访问作为响应发送的数据,我只能看到一个名为codeGET变量和一些长文本。 How do I convert to usable data? 如何转换为可用数据?

I use php for my website 我将php用于我的网站

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

The client_secret parameter must be the App Secret as shows in your app's settings. 如您的应用程序设置中所示,client_secret参数必须是“应用程序密钥”。 The body of the response to this request will be a url encoded string of the form: 此请求的响应主体将是以下形式的url编码的字符串:

access_token=USER_ACESS_TOKEN&expires=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES

You should parse this string and use the access_token value to make requests to the Graph API. 您应该解析此字符串,并使用access_token值向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提出进一步请求,而不必重新验证用户身份。

Once the access token expiry time is reached, the token will become invalid and can no longer be used in requests to the API. 一旦达到访问令牌的到期时间,令牌将变得无效,并且不能再用于对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. 但是,如果用户未取消对您的应用程序的授权,并且您要求的权限不超过用户已授予您的应用程序的权限,则不会显示任何对话框,并且将使用新代码透明地将用户重定向到您的redirect_uri可以交换新的用户访问令牌。

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: 如果在将代码交换为用户访问令牌时遇到问题,授权服务器将发出HTTP 400并将错误作为JSON对象返回到响应的正文中:

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

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

Making requests to the Graph API 向Graph API发出请求

With a valid user access token, you can make requests to read and write data from the Graph API. 使用有效的用户访问令牌,您可以请求从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: 常见的第一个请求是获取刚刚对您的应用进行身份验证的用户的基本信息(包括ID和名称):

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

There is an official SDK for php from Facebook. 有一个来自Facebook的PHP 官方SDK Which makes life easier. 这使生活更轻松。

Check this sample code 检查此示例代码

Use JSON WEB TOKEN(JWT) decoder to get the data which is in the token_id which you will get when you print the contents of GET['code']. 使用JSON WEB TOKEN(JWT)解码器来获取token_id中的数据,该数据将在您打印GET ['code']的内容时获得。 In that select the token_id copy it and paste it in the online decoder 在该选择中,将token_id复制并粘贴到在线解码器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM