![](/img/trans.png)
[英]Accessing logged in user email using facebook javascript sdk
[英]Facebook user email id using javascript SDK
我目前正在使用以下代碼來獲取使用我的應用程序的用戶的電子郵件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文件中,當按下按鈕時調用此函數,我還包括<script src="http://connect.facebook.net/en_US/all.js"></script>
在HTML文件中。 每次我嘗試訪問它時,我的瀏覽器(谷歌瀏覽器)給我以下錯誤“未捕獲的ReferenceError:FB未定義”
我對開發Facebook應用程序比較陌生,所以我所擁有的所有參考都是facebook的開發者頁面,我確信這是一個小錯誤,但我無法繞過它,如果有人能指出我可以指出的話,請欣賞它出錯了
您的代碼無法正確加載。 這部分:
(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
}
祝好運!
你需要把:
(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為你做了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.