我正在努力將Facebook登錄到我的網站上。我不是一個非常強大的JavaScript編碼器,但通常會找到我需要的代碼,並在經過一些工作和試驗和錯誤之後,獲得我需要的結果。無法從Facebook登錄電子郵件登錄
我得到了登錄腳本的工作和我唯一需要的東西(我想 - 記住javascript不是我的強項,所以也許你會在這裏看到一些缺陷,我不這樣做)是從登錄調用中獲取電子郵件地址。我在範圍中添加了「電子郵件」,並嘗試從響應對象以及用戶對象訪問電子郵件。我知道這封電子郵件是有效的,因爲我正在使用我的FB帳戶進行測試,並通過電子郵件進行了驗證,而不是電話。但它沒有定義。
你會看到我試圖在myfacebooklogin函數中獲取它的位置。
任何人都知道/爲什麼我無法訪問電子郵件? 此外,這段代碼看起來是否可靠? 最後,你將如何最好地處理會話/狀態?在ASP中(網站使用的是經典代碼,而不是.NET代碼)我使用會話對象,如果傳統的用戶登錄。如果他們註銷,我只是將會話變量設置爲「」。但是,通過這種方式,您是否僅對每個頁面進行身份驗證並將其用作會話,還是使用FB登錄名創建ASP會話並使用它?
以下是所有的代碼。唯一的其他部分在HTML體中 - myfacebooklogin函數的一個按鈕和一個寫入的div(這是我發現的代碼的一部分,所以我將它留下)。
謝謝!
<script>
// Load the SDK asynchronously
(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'));
window.fbAsyncInit = function() {
FB.init({
appId : 'myAppId',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.8' // use graph api version 2.8
});
// Now that we've initialized the JavaScript SDK, we call
// FB.getLoginStatus(). This function gets the state of the
// person visiting this page and can return one of three states to
// the callback you provide. They can be:
//
// 1. Logged into your app ('connected')
// 2. Logged into Facebook, but not your app ('not_authorized')
// 3. Not logged into Facebook and can't tell if they are logged into
// your app or not.
//
// These three cases are handled in the callback function.
//second parameter of FB.getLoginStatus is "true" to force a roundtrip to FB.
//if "true" not set, result will come from cache
//set "true" only when needed to save performance (for example, on login page)
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
},true);
};
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
//testAPI();
alert('1');
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
alert('2');
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
alert('3');
}
}
function myFacebookLogin() {
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 + '.');
alert(response.name);
alert(response.id);
//alert(response.mail);
});
// FB.api('/me', function(user) {
// console.log(user.name + ': ' + user.email);
// alert(user.email);
// });
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'public_profile, email', return_scopes: true}); //scope: 'publish_actions'
}
</script>
我知道了。我做了兩件事,不知道哪個(或兩者)解決了它。 1)使應用程序生活。 2)在FB.api調用中添加了{fields:'id,name,email'},所以它看起來如下:FB.api('/ me',{fields:'id,name,email'},function回覆) – msith718
但是我仍然喜歡任何優秀的java程序員的反饋,讓我知道如果這對你來說看起來很穩定,如果我錯過了任何東西......以及你將如何處理狀態/會話。 – msith718