嗨,请帮助我运行下面的代码来获取用户的详细信息,如姓名和电子邮件,但是也可以让他们的FB朋友发送电子邮件吗?也许改变这个
FB.api('/ me',{fields:'name,email'} *
进入这个
FB.api('/ me / 朋友',{字段:'姓名,电子邮件'} ?
并在控制台中将其显示为这样的数组?
[{email: 'someone0@email.com'},
{email: 'someone1@email.com'},
{email: 'someone2@email.com'},
{email: 'someone3@email.com'}]
您可以从此处测试示例页面:https://domain/referral
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxxxx', // Set YOUR APP ID
// channelUrl : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response)
{
if (response.status === 'connected')
{
document.getElementById("message").innerHTML += "<br>Connected to Facebook";
//SUCCESS
}
else if (response.status === 'not_authorized')
{
document.getElementById("message").innerHTML += "<br>Failed to Connect";
//FAILED
} else
{
document.getElementById("message").innerHTML += "<br>Logged Out";
//UNKNOWN ERROR
}
});
};
function Login()
{
FB.login(function(response) {
if (response.authResponse)
{
getUserInfo();
} else
{
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'email,user_photos,user_videos,user_friends'});
}
function getUserInfo() {
FB.api('/me',{fields: 'name,email'}, function(response) {
var str="<b>Name</b> : "+response.name+"<br>";
str +="<b>Email:</b> "+response.email+"<br>";
str +="<input type='button' value='Get Friends' onclick='getFriends();'/>";
str +="<input type='button' value='Get Photo' onclick='getPhoto();'/>";
str +="<input type='button' value='Logout' onclick='Logout();'/>";
document.getElementById("status").innerHTML=str;
});
}
function getFriends() {
FB.api('/me/friends',{fields: 'name,email'}, function(response) {
if(response.data) {
$.each(response.data,function(index,friend) {
console.log(friend.name + ' has email:' + friend.email);
});
} else {
alert("Error!");
}
});
}
function getPhoto(){
FB.api('/me/picture?type=normal', function(response) {
var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
document.getElementById("status").innerHTML+=str;
});
}
function Logout() {
FB.logout(function(){document.location.reload();});
}
// 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>
<div align="center">
<h2>Facebook OAuth</h2>
<div id="status">
<img src="http://hayageek.com/examples/oauth/facebook/oauth-javascript/LoginWithFacebook.png" style="cursor:pointer;" onclick="Login()"/>
</div>
<br/><br/><br/><br/><br/>
<div id="message">
Logs:<br/>
</div>
</div>
答案 0 :(得分:1)
如果我按照https://developers.facebook.com/tools/explorer/
所做的操作响应facebook给出了
查询
curl -i -X GET "https://graph.facebook.com/v2.10/me/friends?access_token=<accesstoken sanitized>"
响应
{ "data": [], "summary": { "total_count": 8 }, "__debug__": { "messages": [ { "link": "https://developers.facebook.com/docs/apps/changelog#v2_0", "message": "Only friends who installed this app are returned in API v2.0 and higher. total_count in summary represents the total number of friends, including those who haven't installed the app.", "type": "info" } ] } }
来自Graph API Explorer的调试信息
你能看到
吗?只有安装此应用的朋友才会在API v2.0及更高版本中返回。 &GT;摘要中的total_count表示总数 朋友的数量,包括尚未安装应用的朋友
所以你不能这样的朋友。 就像@CBroe说
一样用户的电子邮件只有在他们个人授予您的应用程序权限后才能访问。