2012-06-01 44 views
2

的朋友我有这个代码的帖子到用户的墙:发布照片和标记使用Facebook的图形API

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) 
       }); 
      }); 

回答

3

我终于成功地破解它,这是一个不同的呼叫比我用的是什么:

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); 
     } 
     }); 
    } 
}); 
+1

如果你发现你的问题的答案,你必须选择它作为正确的! – Philip

+1

我知道,但你不能这样做,直到2天过去了 – Reina

1

你不能上传和标签朋友在同一个呼叫,你必须先上传,然后标记的朋友。如果有更多的则是对朋友,那么你必须通过一个使用循环来标记它们一个,另一个会它会无法正常工作,

+0

我设法做到这一点!将发布我的答案 – Reina

1

我开始用这篇文章中的代码来标记照片中的多个人。它在我的代码库中工作,我试图提炼它,但它可以使用更多的工作,不确定。认为它可能有助于有人试图做同样的事情。

如果任何人有任何改进的想法我所有的耳朵:

//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'); 
       } 
      }); 
     } 
    }); 
}