我正在尝试在网站上实现 Facebook Connect。我正在尝试使用 Facebok 的 Javascript SDK。我是新手,不幸的是,Facebook WIKI 中提供的大部分链接都已过时...返回 404 未找到。无论如何,我在结尾 </body>
之前添加了这段代码:
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<div id="fb-root"></div>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"> </script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '12344', // my real app id is here
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false // parse XFBML
});
FB.login(function(response) {
if (response.session) {
if (response.perms) {
alert('user is logged in and granted some permissions');
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
alert('user is logged in, but did not grant any permissions');
// user is logged in, but did not grant any permissions
}
} else {
alert('user is not logged in');
// user is not logged in
}
}, {perms:'email'});
</script>
我在同一页面的其他地方(比上述脚本早很多)有一个登录按钮:
<fb:login-button v="2">Connect with Facebook</fb:login-button>
此按钮呈现为普通的 fb 连接按钮,单击它会正常打开一个新的弹出窗口。问题是它显示“指定的 api key 无效”错误。在检查弹出窗口的地址栏时,我看到传递给它的是 api_key=undefined :(
对此有什么想法吗?我最近 5 个小时都在尝试解决此问题...请帮我找出为什么没有将正确的 API key 传递到弹出窗口的原因。
提前致谢。
最佳答案
我正在做同样的事情,我找到了一个非常易于实现和理解的示例(这里使用 jQuery,但您可以在没有库的情况下做同样的事情):
<body>
<div>
<button id="login">Login</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({ appId : '12344' });
// fetch the status on load
FB.getLoginStatus(handleSessionResponse);
$('#login').bind('click', function() {
FB.login(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>
</body>
关于javascript - Facebook 连接中的 API key 错误 (javascript sdk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719673/