Reina
Reina

Reputation: 315

Posting a photo and tagging a friend using Facebook Graphs API

I have this code which posts to the user's wall:

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

The first part works perfectly I just cannot figure out how to tag a friend into it, my tag call gives me an empty array back. Facebook documentation is really difficult to understand and it doesn't really give any examples of how to do this so please don't just give me a link to their documentation because I've already read anything they have that's relevant and I still can't do it.

Also tried this with no success:

FB.api('/me', function(response){
                var userId = response.id;
                FB.api('/'+response.id+'/tags/'+userId, {
                    to: $("#recipientID").val()
                }, function(response){
                    console.log(response)
                });
            }); 

Upvotes: 2

Views: 7518

Answers (3)

Myke Preuss
Myke Preuss

Reputation: 23

I started with the code in this post to tag multiple people in a photo. It works in my code base, I've tried to distill it but it could use some more work, not sure. Figured it might help someone trying to do the same thing.

If anyone has any ideas for improvement I'm all ears:

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

Upvotes: 1

Reina
Reina

Reputation: 315

I finally managed to crack it, it's a different call than what I was using:

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

Upvotes: 3

Raquibul Islam
Raquibul Islam

Reputation: 192

you cant upload and tag friends in the same call , you have to upload first , then tag the friends . if there is more then on friend then you have to tag them one by one using loop , other will it'll not work ,

Upvotes: 1

Related Questions