客户端JavaScript Facebook OAuth2实现?

时间:2012-07-13 15:35:31

标签: javascript oauth-2.0 client-side facebook-login digest-authentication

RFC 2617 指定 HTTP基本和摘要式身份验证标准,并按照Wikipedia's "Example with explanation" subsection中的摘要进行操作:

  
      
  • 客户端要求提供需要身份验证的页面,但不提供用户名和密码。通常这是因为用户只需输入地址或链接到页面。
  •   
  • 服务器响应401“客户端错误”响应代码,提供身份验证领域和随机生成的一次性使用的值,称为nonce。
  •   
  • 此时,浏览器将向用户显示身份验证领域(通常是正在访问的计算机或系统的描述),并提示输入用户名和密码。用户可以决定在此时取消。
  •   
  • 一旦提供了用户名和密码,客户端就会重新发送相同的请求,但会添加一个包含响应代码的身份验证标头。
  •   
  • 在此示例中,服务器接受身份验证并返回页面。如果用户名无效和/或密码不正确,服务器可能会返回“401”响应代码,客户端会再次提示用户。
  •   
     

注意:客户可能已经拥有所需的用户名和密码,而无需提示用户,例如:如果它们之前已被网络浏览器存储过。

我正在使用由OAuth2 draft-standard实施的Facebook执行登录。

鉴于服务器具有公开的公共API(例如:JSONRPC或REST),如何编写客户端JavaScript前端以包含前面提到的RFC 2617示例的功能,但是对于Facebook身份验证吗

1 个答案:

答案 0 :(得分:1)

我找到了一个示例客户端JavaScript库实现示例click here for most recent fork的官方(但未维护且现已正式放弃)的存储库。这是该页面的JQuery示例:

<head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <title>Connect JavaScript - jQuery Login Example</title>
  </head>
  <body>
      <h1>Connect JavaScript - jQuery Login Example</h1>
      <div>
          <button id="login">Login</button>
          <button id="logout">Logout</button>
          <button id="disconnect">Disconnect</button>
      </div>
      <div id="user-info" style="display: none;"></div>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    </script>
  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js"></script>
  <script>
      // initialize the library with the API key
      FB.init({
          apiKey: '48f06bc570aaf9ed454699ec4fe416df'
      });


      // fetch the status on load
      FB.getLoginStatus(handleSessionResponse);

      $('#login').bind('click', function () {
          FB.login(handleSessionResponse);
      });


      $('#logout').bind('click', function () {
          FB.logout(handleSessionResponse);
      });

      $('#disconnect').bind('click', function () {
          FB.api({
              method: 'Auth.revokeAuthorization'
          }, function (response) {
              clearDisplay();
          });
      });

      // no user, clear display
      function clearDisplay() {
          $('#user-info').hide('fast');
      }

      // handle a session response from any of the auth related calls
      function handleSessionResponse(response) {
          // if we dont have a session, just hide the user info
          if (!response.session) {
              clearDisplay();
              return;
          }

          // if we have a session, query for the user's profile picture and name
          FB.api({
              method: 'fql.query',
              query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
          },

          function (response) {
              var user = response[0];
              $('#user-info').html('<img src="' + user.pic + '">' + user.name).show('fast');
          });
      }
  </script>

随意提出改进建议。最好我想看看:

  • Vanilla JavaScript实施
  • JSONRPC调用以便于在个人服务器端登录
  • 路由(即:成功登录后重定向到登录用户主页)
相关问题