use*_*230 4 facebook facebook-graph-api facebook-javascript-sdk
我是第一次使用facebook API工作.从那里我发现我们可以获取名称,如下面的console.log(response.name); 但是,我怎样才能获取电子邮件和fbid?
谢谢,Enamul
<?php ?>
<div id="fb-root"></div>
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : 'f', // App ID
channelUrl : '//localhost/practices/phpTest/fblogin/login.php/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
// Additional init code here
};
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
testAPI();
} else {
// cancelled
}
});
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
// Load the SDK Asynchronously
(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));
</script>
<?php ?>
Run Code Online (Sandbox Code Playgroud)
小智 12
除了将scope ="email"添加到按钮之外,还需要指定要返回的字段.
<fb:login-button scope="public_profile, email" onlogin="checkLoginState();" data-auto-logout-link="true" data-size="large">
Run Code Online (Sandbox Code Playgroud)
function testAPI() {
FB.api('/me', {fields: 'name, email'}, function(response) {
console.log( response );
console.log( response.email );
});
}
Run Code Online (Sandbox Code Playgroud)
有关完整的代码结构,请查看facebook文档.
与访问名称的方式相同。IE
response.email和response.id
但要获取电子邮件地址,您必须拥有访问它的权限。
添加scope="email"到您的 FB 登录按钮。
[编辑]
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
testAPI();
} else {
// cancelled
}
}, { scope: 'email' });
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.' + ' Email: ' + response.email + ' Facebook ID: ' + response.id);
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个适合初学者的很好的教程:http://www.loginworks.com/technical-blogs/404-working-with-facebook-javascript-sdk
| 归档时间: |
|
| 查看次数: |
32358 次 |
| 最近记录: |