我目前正在使用以下代码来获取使用我的应用的用户的电子邮件ID
function get_id(cb){
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.login(function(response) {
if (response.authResponse) {
var token = response.authResponse.accessToken;
alert(token);
accessToken = token;
FB.api('/me?access_token=' + token + '', function (response) {
cb(response.email);
});
}
},{scope: 'email'});
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
}; // ending of get_id()
get_id(function (email) {
userID=email;
alert(email);
});
当前代码位于单独的javascript文件中,按下按钮时会调用此函数,我还在HTML文件中包含了<script src="http://connect.facebook.net/en_US/all.js"></script>
。每次我尝试访问它时,我的浏览器(谷歌浏览器)给我以下错误
“未捕获的ReferenceError:FB未定义”
我对开发Facebook应用程序比较陌生,所以我所拥有的所有参考资料都是facebook上的开发者页面,我确信这是一个小错误,但我无法绕过它,如果有人能指出的话,请欣赏它我可能会出错的地方
答案 0 :(得分:3)
您的代码无法正确加载。这部分:
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
不应该在任何函数内部,在身体打开后将其放在脚本标记中。它不能在单独的文件中。这只是all.js的异步加载,即Facebook的JS SDK。如果您使用的是async-load,则不应将<script src="http://connect.facebook.net/en_US/all.js"></script>
放在文档中,因为这会进行同步加载。
代码的另一部分:
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
也不应该在函数内部。这个片段负责在你的页面中启动FB JS SDK,并且你的应用程序信息只需要为每个页面调用一次,否则你将多次启动引擎并且它会产生caotic行为。只有在加载FB JS SDK后才会创建“FB”对象。由于异步负载,你不知道什么时候会发生。因此,您必须将FB.init分配给此窗口事件:
window.fbAsyncInit = function() {
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
您想要创建一个使用Facebook信息的get_id函数。你必须要小心。确保在完全加载FB JS SDK后调用此函数,您可以在页面的任何部分创建它。但我认为最安全的方法是在window.fbAsyncInit中创建它。不要担心令牌,SDK会为您完成:
window.fbAsyncInit = function() {
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
function get_id(cb){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function (response) {
cb(response.email);
});
}
},{scope: 'email'});
}
};
但是当你这样做时,你将不确定你的功能是否已经创建。因此,当你调用它时,你将不得不测试它的存在:
if(get_id && typeof get_id == 'function') {
// It's safe to call your function, go ahead
get_id(function (email) {
userID=email;
alert(email);
});
} else {
// Your function does not exists yet, do other stuff
}
祝你好运!
答案 1 :(得分:1)
你需要把:
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
get_id
函数。所以在按下按钮之前调用它
PS:你不需要手动传递?access_token=
- FB JS SDK为你做了