swa*_*dhi 4 javascript facebook facebook-javascript-sdk facebook-graph-api-v2.0
我正在尝试为我的应用程序获取Facebook用户电子邮件.但是当我查询时,电子邮件地址将返回为undefined.我没有得到如何处理它.
这是我的代码 -
<body>
<script>
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
FB.login(function(response) {}, {
scope: 'email'
});
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
FB.login(function(response) {}, {
scope: 'email'
});
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId: 'app id',
cookie: true, // enable cookies to allow the server to access
xfbml: true, // parse social plugins on this page
version: 'v2.1' // use version 2.1
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
(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'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
alert(response.name);
alert(response.email);
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
});
}
</script>
<fb:login-button perms="email" autologoutlink="true" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
小智 8
@Tobi这真的救了我的一天:
FB.api('/me
Run Code Online (Sandbox Code Playgroud)
取而代之
FB.api('/me?fields=id,name,email,permissions', function(response) {
console.log(response.name);
console.log(response.email);
});
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么以下代码的范围(在facebook文档中)不起作用
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3431 次 |
| 最近记录: |