How can I tag a user in a photo using the Facebook Graph API?

19,199

Solution 1

There is no current mechanism to set tags with the Facebook Graph API.

You can use photos.addTag with the old REST API which you should be able to download here: http://pearhub.org/get/facebook-0.1.0.tgz. If anyone has a better link, I would appreciate it.

Looking at this article from the Facebook developers http://developers.facebook.com/blog/post/371 it seems likely that the new API will never support setting tags "let the users do the tagging". This is unfortunate for apps (like mine) that want to integrate tagging.

Solution 2

Uploading image and tagging multiple people in one step using PHP Facebook Graph API:

$args = array(
  'message' => 'Message',
  'image'   => '@' . realpath($path_to_image),
  'tags'    => array(
     array(
      'tag_uid'=> $friend1_uid,
      'x'      => $x1,
      'y'      => $y1,
     ),
     array(
      'tag_uid' => $friend2_uid,
      'x'       => $x2,
      'y'       => $y2,
     ),/*...*/
   ),
);

$data = $facebook->api('/me/photos', 'post', $args);

Where $facebook is initialized Facebook PHP SDK object. $data['id'] is id of uploaded photo.

Notes: 1. fileUpload option has to be set when initializing Facebook object:

$facebook = new Facebook(array(
  /*..*/
  'fileUpload'  => true,
));
  1. publish_stream permission has to be granted;

Solution 3

Change the url to

$url = "https://graph.facebook.com/{$idPhoto}/photos";

tags is not a compatible method! use photo to retrieve the specified data

Take a look at!

http://developers.facebook.com/docs/reference/api/photo for tagging users in photos.

Share:
19,199
FR6
Author by

FR6

Updated on August 23, 2022

Comments

  • FR6
    FR6 over 1 year

    I tried:

    $args = array(
      'access_token' => $access_token,
      'id' => $uid
    );
    
    $url = "https://graph.facebook.com/{$idPhoto}/tags";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $data = curl_exec($ch);
    

    It has returned me:

    {"error":{"type":"QueryParseException","message":"Unknown path components: \/tags"}}
    

    It does not seem possible because its not in the Facebook documentation:

    http://developers.facebook.com/docs/api#publishing

    Can someone confirm me that it's not possible to tag a user in a recently uploaded photo?