我正在努力将Facebook登录到我的网站上。我不是一个非常强大的JavaScript编码器,但通常会找到我需要的代码,经过一些工作和反复试验,得到我需要的结果。
我的登录脚本正常工作,我唯一需要的东西(我想 - 记住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>
答案 0 :(得分:2)
您需要传递网址"/me"
中的字段,如:
FB.api('/me?fields=name,id,email', function(response) {