嗨,请帮助我运行下面的代码来获取用户的详细信息,如姓名和电子邮件,但是也可以让他们的FB朋友发送电子邮件?也许改变这个
FB.api('/ me',{fields:'name,email'} *
进入这个
FB.api('/ me / friends',{fields:'name,email'}?
并在控制台中将其显示为这样的数组?
[{email: '[email protected]'},
{email: '[email protected]'},
{email: '[email protected]'},
{email: '[email protected]'}]
您可以从这里测试样本页面: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>
你检查过这个吗? https://developers.facebook.com/docs/graph-api/reference/user/
如果我做你在https://developers.facebook.com/tools/explorer/做的事情
响应facebook给出了
Query
curl -i -X GET "https://graph.facebook.com/v2.10/me/friends?access_token=<accesstoken sanitized>"
Response
{ "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" } ] } }
Debug Information from Graph API Explorer
你有没有看到
只有安装此应用程序的朋友才能在API v2.0及更高版本中返回。 > total_count in summary表示朋友的总数,包括尚未安装该应用的朋友
所以你不能得到这样的朋友。就像@CBroe说的那样
用户的电子邮件只有在他们个人授予您的应用程序权限后才能访问。