使用Facebook Graphs API发布照片并标记朋友
我在用户的墙上贴了一个代码:使用Facebook Graphs API发布照片并标记朋友,facebook,facebook-graph-api,Facebook,Facebook Graph Api,我在用户的墙上贴了一个代码: FB.api('/me/photos', 'post', { message:'photo description', url:imgURL }, function(response){ console.log(response); if (!response || response.error) { console.log(response);
FB.api('/me/photos', 'post', {
message:'photo description',
url:imgURL
}, function(response){
console.log(response);
if (!response || response.error) {
console.log(response);
}else{
FB.api(response.id+'/tags/me', {
to: $("#recipientID").val()
}, function(response){
console.log(response)
});
}
});
第一部分工作得很好,我只是不知道如何将朋友标记到其中,我的标记调用返回一个空数组。Facebook文档真的很难理解,它没有给出任何关于如何做到这一点的例子,所以请不要只是给我一个他们文档的链接,因为我已经阅读了他们的任何相关内容,我仍然无法做到
我也尝试过,但没有成功:
FB.api('/me', function(response){
var userId = response.id;
FB.api('/'+response.id+'/tags/'+userId, {
to: $("#recipientID").val()
}, function(response){
console.log(response)
});
});
你不能在同一个通话中上传和标记朋友,你必须先上传,然后标记朋友。如果friend上有更多,那么您必须使用loop逐个标记它们,否则它将不起作用,我终于设法破解了它,这与我使用的调用不同:
FB.api('/me/photos', 'post', {
message:'Checking tags',
url:imgURL
}, function(response){
if (!response || response.error) {
console.log(response);
}else{
//tags friend
var postId = response.id;
FB.api(postId+'/tags?to='+friendID, 'post', function(response){
if (!response || response.error) {
console.log(response);
}
});
}
});
我从这篇文章中的代码开始,在一张照片中标记多个人。它在我的代码库中工作,我试图提取它,但它可能需要更多的工作,不确定。我想这可能有助于尝试做同样事情的人 如果有人有任何改进意见,我洗耳恭听:
//Set empty array of Friend ID's
var friendIds = []
//Get friend ID's
getFriendById = function(id) {
var i, len;
id = id.toString();
for (i = 0, len = friends.length; i < len; i += 1) {
if (friends[i].id === id) {
return friends[i];
}
}
friendIds.push(friends);
};
var postToWall = function(){
//Assign Friends to variables
var name1 = getFriendById(friendIds[0]);
var name2 = getFriendById(friendIds[1]);
var name3 = getFriendById(friendIds[2]);
//Set empty array for tags
var tags = [];
//Loop through friends and make an array ready for posting
$.each(selectfriends, function(i,friend){
var new_tag = {tag_uid: friend};
tags.push(new_tag);
})
//Post photo to wall
FB.api('/me/photos', 'post', {
message:'Enter custom message',
url: 'link/to/photo.jpg'
}, function(response){
console.log(response)
if (!response || response.error) {
console.log('error');
} else {
//Tag Friends
var postId = response.id;
//Use stringify to send the array in string to facebook
FB.api(postId+'/tags?tags='+JSON.stringify(tags), 'post', function(response){
if (!response || response.error) {
console.log('error');
}
});
}
});
}
//设置朋友ID的空数组
var-friendIds=[]
//拿到朋友的身份证
getFriendById=函数(id){
变量i,len;
id=id.toString();
for(i=0,len=friends.length;i
如果您在问题中找到了答案,您必须选择正确的答案!我知道,但是你要过两天才能做