2012-05-31 44 views
0

我有一个使用旧版本FB API的注册表单。如何正确处理用户注册和重定向?

我试图将其更新为当前版本,并且遇到了处理来自FB的响应的问题。

在点击FB登录按钮并授予权限(email,publish_stream)后,我无法找到一种方法来知道用户已授予访问权限(而不仅仅以已注册的用户身份登录),并且需要继续进行下一步注册表格的页面。

一个成功的许可请求后的反应是一样的登录成功:

{ 
    authResponse: 
    { 
     accessToken: AAAFEIew...URYo, 
     userID:   100003908595992, 
     expiresIn:  6014, 
     signedRequest: uhTqZodDn0QUoWAUBuYEKmlaM8zViO0r_fnKONaC4v8.eyJhbGdvcml...k5MiJ9, 
    }, 
    status: connected 
} 

我怎样才能知道如果登录已注册用户或新注册的结果呢?

我使用的是JavaScript SDK。

代码:

<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script> 


       <div id="fb-root" style="padding: 0px;"></div> 
       <script> 
        window.fbAsyncInit = function() { 
         FB.init({ 
          appId  : 'API_KEY', 
          status  : true, 
          channelUrl : '//'+window.location.hostname+'/channel.php', 
          cookie  : true, 
          xfbml  : true, 
          oauth  : true 
         }); 

         // listen for and handle auth.statusChange events 
         FB.Event.subscribe('auth.statusChange', function(response) { 
          if (response.authResponse) { 
           // user has auth'd your app and is logged into Facebook 
           alert('logged in!'); 
          } 
          else 
          { 
           // user has not auth'd your app, or is not logged into Facebook 
           alert('not logged in!'); 
          } 
         }); 

        }; 
        (function(d){ 
         var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} 
         js = d.createElement('script'); js.id = id; js.async = true; 
         js.src = "//connect.facebook.net/en_US/all.js"; 
         d.getElementsByTagName('head')[0].appendChild(js); 
        }(document)); 

回答

0

我意识到我需要使用getLoginStatus功能在FB SDK。

我的功能代码是:

<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : '<?php echo HSWI_FB_KEY; ?>', 
      status  : true, 
      logging  : true, 
      channelUrl : '//'+window.location.hostname+'/channel.php', 
      cookie  : true, 
      xfbml  : true, 
      oauth  : true 
     }); 

     FB.Event.subscribe('auth.login', function(response) 
     { 
      window.location.href = '/login_script.php'; 
     }); 

     FB.Event.subscribe('auth.logout', function(response) { 
      window.location.reload(); 
     }); 

     checkLoggedInFB(); 

    }; 
    (function(d){ 
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     d.getElementsByTagName('head')[0].appendChild(js); 
    }(document)); 

    function fbLogin() 
    { 
     FB.login(
      function(response) 
      { 
       if(response.authResponse) 
       { 
        window.location.href = '/login_script.php'; 
       } 
      } 
     ); 
    } 

    function checkLoggedInFB() 
    { 
     try 
     { 
      FB.getLoginStatus(
       function(response) 
       { 
        if(response.status == 'connected') 
        { 
         window.location.href = '/login_script.php'; 
        } 
       } 
      ); 
     } 
     catch(e) 
     { 
      alert('Error checking if logged into FB: '+ e.message); 
      return false; 
     } 
     return true; 
    } 

</script> 
<style type="text/css"> 
    .fb-login-button 
    { 
     margin-top: -15px; 
    } 

    .fb-login-button.fb_iframe_widget.fb_hide_iframes 
    { 
     display: none; 
    } 
</style> 
<div class="fb-login-button" scope="email,publish_stream,user_birthday,user_location,user_about_me">Login</div> 
0

这是试图做你在做什么,当我使用的资源。它工作得非常好。

window.fbAsyncInit = function() { 
    FB.init({ appId: 'Your_APP_ID', 
     status: true, 
     cookie: true, 
     xfbml: true, 
     oauth: true}); 

    showLoader(true); 

    function updateButton(response) { 
     button  = document.getElementById('fb-auth'); 
     userInfo  = document.getElementById('user-info'); 

     if (response.authResponse) { 
      //user is already logged in and connected 
      FB.api('/me', function(info) { 
       login(response, info); 
      }); 

      button.onclick = function() { 
       FB.logout(function(response) { 
        logout(response); 
       }); 
      }; 
     } else { 
      //user is not connected to your app or logged out 
      button.innerHTML = 'Login'; 
      button.onclick = function() { 
       showLoader(true); 
       FB.login(function(response) { 
        if (response.authResponse) { 
         FB.api('/me', function(info) { 
          login(response, info); 
         }); 
        } else { 
         //user cancelled login or did not grant authorization 
         showLoader(false); 
        } 
       }, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'}); 
      } 
     } 
    } 

    // run once with current status and whenever the status changes 
    FB.getLoginStatus(updateButton); 
    FB.Event.subscribe('auth.statusChange', updateButton); 
}; 
(function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol 
     + '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
}()); 

参考:http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/

信仰斯隆