2014-11-08 147 views
6

我想通過Facebook Javascript SDK將Facebook登錄整合到我的網站中。根據步驟由Facebook開發文檔here提供的操作說明,這裏是測試代碼中,我寫道:FB.login裏面的FB.getLoginStatus彈出窗口被阻止

<script> 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '{$MY_APP_ID}', 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true, // parse social plugins on this page 
     version : 'v2.1', // use version 2.1 
     status  : true, // check FB login status 
    }); 
}; 

function fblogin() { 
    FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
     alert('Logged in.'); 
     } 
     else { 
     FB.login(); 
     } 
    }, true); 
} 
</script> 
<button onclick="fblogin()">login</button> 
<script src="//connect.facebook.net/en_US/sdk.js"></script> 

很抱歉,由於網站域名的限制,我不能拿這個去撥弄。但我嘗試了自己的域名,Facebook和Chrome瀏覽器登錄窗口,但被Safari阻止。這有點奇怪,Facebook Developer Document提供的代碼片段有什麼問題嗎?

+1

是很常見的現代瀏覽器來阻止自動彈出窗口 - 自動的意思,他們沒有像點擊任何用戶交互觸發。後者也是建議 - 僅在點擊時調用「FB.login」。這使得彈出窗口很可能會被阻止。除此之外,他們還會留下_choice_是否想通過用戶手中的FB登錄 - 而不是將其推送到他們手中。 – CBroe 2014-11-08 12:21:42

+0

@CBroe然後我認爲Facebook應該在用戶開始選擇這個選項之前在他們的Javascript SDK教程中提到這個限制。順便說一句,我只嘗試用戶FB.login用戶單擊操作,但有一些限制,如'授權碼已被使用'錯誤發生在用戶嘗試登錄兩次。 – David 2014-11-08 12:31:32

+0

那麼請不要試圖登錄兩次......'FB.getLoginStatus'告訴你他們是否已經在頁面加載時登錄 - 如果是這樣,不要顯示登錄鏈接,而是顯示「hello {name}」或者相反。我認爲Facebook提到了在最佳實踐中沒有用戶交互的調用問題......但上述代碼僅僅是一個簡短的例子,而_examples_的本質並不是涵蓋所有可能的問題。 – CBroe 2014-11-08 12:41:20

回答

10

正如我們在評論中已經討論過的,FB.login必須在用戶交互中調用,文檔中的示例不正確。

這應該是如何正確地做一個登錄工作的例子,從幾篇文章在Facebook的文檔一起復制:

<script> 
    //call this on user interaction (click) 
    function doLogin() { 
     FB.login(function(response) { 
      if (response.authResponse) { 
       console.log('Welcome! Fetching your information.... '); 
       FB.api('/me', function(response) { 
        console.log('Good to see you, ' + response.name + '.'); 
       }); 
      } else { 
       console.log('User cancelled login or did not fully authorize.'); 
      } 
     }, {scope: 'email,user_friends'}); 
    } 

    window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : 'your-app-id', 
      xfbml  : true, 
      version : 'v2.1' 
     }); 

     FB.getLoginStatus(function (response) { 
      if (response.status === 'connected') { 
       // the user is logged in and has authenticated your 
       // app, and response.authResponse supplies 
       // the user's ID, a valid access token, a signed 
       // request, and the time the access token 
       // and signed request each expire 
       var uid = response.authResponse.userID; 
       var accessToken = response.authResponse.accessToken; 
      } else if (response.status === 'not_authorized') { 
       // the user is logged in to Facebook, 
       // but has not authenticated your app 
      } else { 
       // the user isn't logged in to Facebook. 
      } 
     }); 
    }; 

    (function(d, s, id){ 
     var js, fjs = d.getElementsByTagName(s)[0]; 
     if (d.getElementById(id)) {return;} 
     js = d.createElement(s); js.id = id; 
     js.src = "//connect.facebook.net/en_US/sdk.js"; 
     fjs.parentNode.insertBefore(js, fjs); 
    }(document, 'script', 'facebook-jssdk')); 
</script> 
<button onclick="doLogin()">login</button> 

更多的解釋:http://www.devils-heaven.com/facebook-javascript-sdk-login/

+0

這是否仍然有效?似乎正在獲得標準的「不受支持的瀏覽器」消息。 – Dano007 2015-07-08 16:24:30

+0

是的,我每天都在使用它。不確定你在說什麼信息?你需要更具體。 – luschn 2015-07-08 16:26:01

+0

你有我可以訪問的示例網站嗎?我已經將上面的代碼複製到我的,但仍然得到錯誤「不支持的瀏覽器:谷歌瀏覽器IOS不支持此功能。使用Safari瀏覽器」我用我的iPhone測試 – Dano007 2015-07-08 19:11:02

相關問題