2012-02-08 50 views
1

我正在嘗試在我的網站上實現Facebook登錄功能。我按照說明操作,並且儘可能正確地輸入了代碼。這裏是我的代碼:Facebook在我的網站上登錄:「發生錯誤,請稍後再試。」

<div id="fb-root"></div> 
<div class="fb-login-button" data-scope="email" >Login with Facebook</div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
     appId  : 'MY_APP_ID', 
     status  : true, 
     cookie  : true, 
     xfbml  : true, 
     oauth  : true, 
     }); 
    }; 
    (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)); 

如果你看到的東西我不知道,請告訴我。這應該工作據我所知(按照facebook的說明)後。

感謝您的幫助提前。

+0

請更新腳本關閉。 – phwd 2012-02-08 06:59:06

回答

0

我假設你用你的實際ID替換MY_APP_ID,然後關閉腳本部分。如果這不是問題,那麼除了您提供的代碼片段之外,我還需要查看更多HTML源代碼。

以下是一個html頁面源代碼示例,它有一個登錄/註銷按鈕,具體取決於您當前是否登錄。它還會顯示登錄時登錄的用戶個人資料圖片。這是我基於Facebook文檔創建的一個示例,但我測試了它並且它可以正常工作。顯然你需要用一個實際的應用程序ID來替換應用程序ID。

<html> 
<head> 
    <title>Hello World</title> 
    <meta name="viewport" content="initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> 
</head> 
<body> 

<!-- Display a login if you're coming to the app from outside of facebook --> 
<div id="login"> 
<p><button onClick="loginUser();">Login</button></p> 
</div> 

<div id="logout"> 
    <p><button onClick="FB.logout();">Logout</button></p> 
</div> 

<script> 
    function loginUser() {  
    FB.login(function(response) { }, {scope:'email'});  
    } 
</script> 

<p>Misc page content here...</p> 

<!-- Initialize the fb javascript SDK --> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({ appId: '1234567...', 
       status: true, 
       cookie: true, 
       xfbml: true, 
       oauth: true}); 
    FB.Event.subscribe('auth.statusChange', handleStatusChange); 
    }; 
</script> 

<!-- Callback triggered when the user authenticates --> 
<script> 
    function handleStatusChange(response) { 
     document.body.className = response.authResponse ? 'connected' : 'not_connected'; 
     if (response.authResponse) { 
     console.log(response); 

     updateUserInfo(response); 
     } 
    } 
</script> 

<!-- fb javascript SDK import --> 
<div id="fb-root"></div> 
<script> 
    (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> 

<!--Display the user's face when they log in --> 
<div id="user-info"></div> 
<script> 
    function updateUserInfo(response) { 
    FB.api('/me', function(response) { 
     document.getElementById('user-info').innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name; 
    }); 
    } 
</script> 

<!-- This style sheet changes the name of the body to reflect logged in status, thereby 
    cueing to the login logout buttons which one should be visible --> 
<style> 
    body.connected #login { display: none; } 
    body.connected #logout { display: block; } 
    body.not_connected #login { display: block; } 
    body.not_connected #logout { display: none; } 
</style> 
</body> 
</html> 
+0

是的,腳本關閉確實被切斷了,MY_APP_ID是作爲我實際APP ID(我輸入的)的佔位符。 – 2012-02-08 06:53:15

+0

約瑟夫,我用你的代碼,我得到了同樣的錯誤。我不知道什麼似乎是問題。 – 2012-02-08 19:19:00

+0

好的,我在基本頁面上輸入了我的「APP域名」主頁url,現在它只是立即打開並關閉 – 2012-02-08 19:30:01

2

我想通了,當我有同樣的問題。 oauth:true之後不應有,。見下面

FB.init({ 
    appId  : 'MY_APP_ID', 
    status  : true, 
    cookie  : true, 
    xfbml  : true, 
    oauth  : true, //',' here is causing the problem 
}); 
相關問題