8

I'm developing an app for Facebook in PHP, part of which lists the user's "likes". I would like to add a link next to each like so that the user can manage their likes by deleting them where they see fit.

Facebook mentions this in their graph api docs:

You can delete a like by issuing a DELETE request to /POST_ID/likes (since likes don't have an ID).

But each like must have an id - how else would you delete it?

Has anyone done this before?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Martin
  • 10,294
  • 11
  • 63
  • 83

5 Answers5

5

Yes, likes don't have an ID in the Graph API. You like or unlike an item by POSTing or DELETEing to {item_id}/likes, where {item_id} is replaced by the ID of the object you're liking/unliking.

To find out what the current user has liked (so you can delete them appropriately), you can use the "likes" connection of the User object (docs). So, if you request http://graph.facebook.com/me/likes, you'll get a list of pages/people/whatever that a user has liked. (Note: this does not include posts or photos or things like that)

This will return an array of data full of items like this:

{
     "name": "Very Hungry Caterpillar",
     "category": "Artist",
     "id": "29956247793",
     "created_time": "2009-03-27T15:48:29+0000"
}

The ID in there is not the ID of the like. It is the ID of the object that the user has liked, so in order to un-like it, you have to make a DELETE to http://graph.facebook.com/29956247793/likes.

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • I just tried to do this via Facebook's graph API explorer and it didn't work for me. It gave the error: { "error": { "message": "(#3) Application does not have the capability to make this API call.", "type": "OAuthException", "code": 3 } } Is this method still working for you? – Mike Howsden Dec 03 '14 at 18:39
  • @Mike - which permissions did you request for your app? Your error sounds like you might be missing a permission. (And yes, it worked for me two years ago when I wrote this answer, but I haven't tested it since) – cloudfeet Dec 05 '14 at 17:03
  • I granted all permissions possible when I tried it but paid special attention to publish_actions. No luck. Either I'm doing something wrong or the graph API explorer has limited functionality or Facebook no longer supports this. – Mike Howsden Dec 05 '14 at 19:40
  • I've the same error { "error": { "message": "(#3) Application does not have the capability to make this API call.", "type": "OAuthException", "code": 3 } }. It's possible to delete likes by API ? – R.Kueny Jan 28 '15 at 18:38
5

It's not the 'like' that has an ID, it's the post - which is why the api call uses '/POST_ID/likes' as a target - if you delete '/POST_ID', it'll get rid of the post, but if you delete '/POST_ID/likes' it'll get rid of the user's 'like' for that post.

matt lohkamp
  • 2,174
  • 3
  • 28
  • 47
1

With OpenGrpah, likes do have ids, it is the single id field in the object returned by an API call to og.likes.

Samuel
  • 2,106
  • 1
  • 17
  • 27
1

Likes do have an ID.

If you look at https://graph.facebook.com/me/likes, you will see that the resulting data does contain an ID value for each.

{
   "data": [
      {
         "name": "Audi",
         "category": "Consumer_products",
         "id": "96585976469",
         "created_time": "2010-09-27T15:30:15+0000"
      }
    ]
}

You might want to try the ID's there, I've noticed that the FB API doc sometimes has errors.

Edit: I think this also may be a terminology issue, as what the doc says doesn't have ID's is probably likes to a user post, and these probably don't really have an ID and can be removed by issuing a delete to the POST_ID/likes. Then there's the likes generated by liking pages and/or external websites via the like-button, and these DO have an ID. Confusing, it is.

TuomasR
  • 2,296
  • 18
  • 28
  • Thanks TuomasR, I can get the likes as you specify using $likes = $facebook->api('/me/likes'); which returns an object similar to that you have displayed above. I'm wondering what url or fb javascript method would be used to remove a specific like? Maybe the best way to do this is to reverse engineer the like and get the url of the like like so: $likeParams = json_decode(file_get_contents('https://graph.facebook.com/?ids='.$like['id'])); $likeParams->{$like['id']}->link; and then add a like button for each. Here you could choose to unlike the item. – Martin Oct 01 '10 at 08:23
  • 1
    That's not an ID for the "like", that's the ID of the object being "liked". So, to unlike that object, you would have to make a DELETE to `https://graph.facebook.com/96585976469/likes". – cloudfeet Sep 01 '12 at 07:03
1

You unlike using access_token which used to make this like.

Pseudocode:

to like:

FacebookGraphApi::getInstance()->setAccessToken('xxx')->post('xxxxxx/likes')

to unlike:

FacebookGraphApi::getInstance()->setAccessToken('xxx')->delete('xxxxx/likes')
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
zhenyka
  • 11
  • 1