FB.login 10月需要进行Javascript更改?

时间:2011-08-29 20:51:30

标签: javascript facebook oauth facebook-javascript-sdk oauth-2.0

因此,我们在Facebook Connect首次推出时从Facebook Connect切换到新的Javascript SDK和OpenGraph。

早在5月份,我们的一些客户收到了一封电子邮件,告诉他们可能存在安全漏洞,可能需要升级到Oauth 2.0。我查看了我们的新代码与当时FB.login的文档相比较,最终给人的印象是使用我们的新东西的客户会好的,所以我们产品的旧Facebook Connect版本上的客户将不得不升级到最新版本。

今天我注意到Javascript SDK已经更改,因此要使用OAuth 2.0,毕竟需要进行代码更改。 (即this blog post,这是在电子邮件发出一个月后制作的),而且我需要在10月1日之前升级。

所以,今天我尝试将我的应用程序的“Oauth 2.0 Migration”标志设置为true并使用相同的代码运行它。它工作,我没想到。所以我的问题是,我是否需要在链接的博客文章中进行代码更改?如果今天应用程序选中“Oauth 2.0 Migration”复选框,那么这个有效原因是否会在10月1日之后继续有效?

这是我的代码:

// call to FBinit does not include oauth: true
FB.init({appId: opts.ApiKey, status: true, cookie: true, xfbml: true});

// call to login expects response.session on response. not response.authResponse. 
// Shame on Facebook for arbitrarily renaming that so I can't do a clean swap.
FB.login(function(response){
    if(response.session){
        var access_token = response.session.access_token;
        // blah blah blah
    }
});

3 个答案:

答案 0 :(得分:1)

是的,您需要对JS SDK(http://developers.facebook.com/docs/oauth2-https-migration/)进行代码更改,以在FB.init函数中包含oauth:true并且博客文章中提到的其他更改。

Dev App中的迁移设置仅表示您将收到加密的访问令牌(请参阅工具提示)。

答案 1 :(得分:0)

我会更改所有代码,同时启用O-2.0并使用旧的auth方法可能会破坏会话,导致用户无法通过应用程序注销,或者在用户退出Facebook时在应用程序中离开会话。


启用新的O-2.0并禁用旧的auth后,我使用下面的示例,与php-sdk 3.1.1集成,没有任何错误或问题。

      <div id="FBauth"></div>
      <div id="fb-root"></div>
<script>
      window.fbAsyncInit = function() {
        FB.init({
    appId  : '112104298812138',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    //channelUrl : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
    oauth  : true // enable OAuth 2.0
        });
FB.Canvas.EarlyFlush.addResource("http://shawnsspace.com/index.php");
FB.Canvas.setAutoResize();
            FB.getLoginStatus(function(response) {
              if (response.authResponse) {
                // logged in and connected user, someone you know
                var authbox = document.getElementById('FBauth');
                //authbox.innerHTML="Hey" +authResponse.name+ "";
                authbox.innerHTML="<fb:login-button autologoutlink='true'></fb:login-button><fb:login-button show-faces='true' width='250' max-rows='1'></fb:login-button>";
                FB.XFBML.parse(authbox);
                //var a = document.createElement('a');
                //alert();
              } else {
                // no user session available, someone you dont know
                var authbox = document.getElementById('FBauth');
                authbox.innerHTML="";
                var a = document.createElement('a');
                a.setAttribute("href","javascript:void();");
                a.setAttribute("onclick","FBlogin();");
                a.innerHTML="Please Login";
                authbox.appendChild(a);
                //alert('not logged in'+response+'');
//
        window.FBlogin = function(){
                FB.login(function(response) {
               if (response.authResponse) {
                 FB.api('/me', function(response) {
                 });

               } else {
               top.location.href = "http://apps.facebook.com/shawnsspace/";
                 // user cancealed login.
               }
             }, {scope: 'manage_pages'});
        };
//          
              }
            }); 

        FB.Event.subscribe('auth.login', function(response) {
        top.location.href = 'http://apps.facebook.com/shawnsspace/';
        });
        FB.Event.subscribe('auth.logout', function(response) {
        //top.location.href = "http://facebook.com/designbyshawn";
        });
      };
      (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);
      }());
</script>

答案 2 :(得分:0)

上面的代码没有使用Oauth2。它会一直有效,直到他们决定强迫你使用Oauth2。

要切换,您需要将oauth:true添加到FB.init调用中,如您引用的博客文章中所述。主要的变化是response.session成为response.authResponse

还有很多其他变化,所以我建议现在测试一下。我花了几个小时来转换我们的网站,但大部分都是让cookie工作,因为它们与Oauth2完全不同,我们想从cookie中生成一个access_token。

您可以查看我为Oauth2更新的Rails插件 - https://github.com/imme5150/fgraph Cookie代码位于此处:https://github.com/imme5150/fgraph/blob/master/lib/fgraph/rails/fgraph_helper.rb位于底部。另一个技巧是从存储在cookie中的“code”参数中获取访问令牌,您可以调用FB图,但是您必须包含“redirect_uri”参数,但是您希望它是空白的。

祝你好运!