72

Using the Graph API I'd like to be able to have an authorized user "like" a page.

I tried posting the following

https://graph.facebook.com/${PAGE_ID}/likes?access_token=${ACCESS_TOKEN}

And I get an HTTP error 500 accompanied by "Invalid post_id parameter" in the JSON response body. Looks like the /likes resource is suited to liking a wall post and not a page. How do I get this to work with a page?

Kara
  • 6,115
  • 16
  • 50
  • 57
Tom Wells
  • 1,159
  • 2
  • 9
  • 15

6 Answers6

43

Facebook has announced support for liking URL's outside of Facebook by using the official built-in Like action. You need to have publish_actions permissions. The graph url for this is: https://graph.facebook.com/[User FB ID]/og.likes?object=OG_OBJECT_URL&access_token=USER_ACCESS_TOKEN

However, you cannot use this to like a page on Facebook currently, as the documentation states:

For Facebook Pages or websites that do not integrate with Facebook Authentication, developers should continue to use the Like button social plugin.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
bkaid
  • 51,465
  • 22
  • 112
  • 128
  • I dont think you can like a page using the built-in like cation. Can you confirm? – AbhinavVinay Jul 03 '12 at 19:53
  • Is there a facebook developer resource that says you can't use Built-in like's on facebook pages. Not that I don't believe you, but I'm trying to understand why it is the case. Are you referring to this statement on the built-in like doc page: "The usage of the built-in Like action by an app, as with any Open Graph action, needs to be submitted and approved. For Facebook Pages or websites that do not integrate with Facebook Authentication, developers should continue to use the Like button social plugin."? – Sean Glover Sep 13 '12 at 14:32
  • This is the error message returned from the graph API when I attempted to like a facebook page: "(#100) Like actions are not yet supported against objects of this type." – Sean Glover Sep 13 '12 at 16:19
  • @BK. Does this actually mean that there is no other way of liking a facebook page than using the like button plugin? – Mihai Bujanca Mar 16 '13 at 18:32
  • 2
    How can I find out the OG_OBJECT_URL from a page that would I like? – deldev Mar 18 '14 at 22:03
11

Update June 2016

It's still not possible to like a page using Facebook API, as stated in the /{user_id}/likes documentation page about Creating/Updating/Deleting:

You can't perform this operation on this endpoint.

In previous versions the message was clearer (see the quote below), but the result is the same: it's not possible.

May 2014

The /{user-id}/likes documentation page States about Publishing Likes of Facebook Pages:

You can't publish using this edge, as it is not possible to like a Facebook Page via any API. You should use the Like Button if you want people to be able to like a page in your app.

This is the most obvious and clear statement that has been able to give me an answer to the question.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
5

if your app is an open graph app, now you can like using the api, and no need for the button anymore.

https://developers.facebook.com/docs/opengraph/actions/builtin/likes/

Israhack
  • 459
  • 1
  • 6
  • 15
  • 5
    As mentioned in the docs and in BK's answer, this doesn't work for Facebook Pages, only for other objects – Igy Oct 31 '12 at 00:19
5

If you want this functionality in a page tab or canvas page within facebook (say to allow for liking the page from within a likegated page), a work around you can involves what Tom Wells suggested in his reply to Luke. You first embed the iframe version of their like button on your page, and then simply listen for the edge.create event in your JS like so:

FB.Event.subscribe('edge.create',
    function(response) {
        alert('You liked the URL: ' + response);
        // ...
    }
);

In the callback, you can deal with with what happens when the user has liked the page, say like navigating away from the like-gate page, or showing liked-only content.

When the user clicks the iFrame like button, your JS code should receive the edge.create event assuming the iFrame was configured to point to the url of the page in question.

sakibmoon
  • 2,026
  • 3
  • 22
  • 32
Keith
  • 61
  • 1
  • 1
  • 1
    I think the OP is asking how to do this programatically, perhaps as a way of passing a "like" through a custom control. – James P. Sep 09 '12 at 00:23
3

I believe this is not allowed except for specific partner sites, like yelp. The reason is security, you would be able to put some javascript on a page and have everyone that visits that page "Like"ing it without their knowledge.

See How do I "Like" a URL? on the Facebook Platform Developer Forum

Luke
  • 202
  • 3
  • 10
  • 2
    Well not really - the graph api makes use of an access token to prevent that sort of security problem by asking the user to explicitly give access to the facebook application. Anyway I solved the issue by going facebook's preferred route of embedding an iframe to their like button (open graph api). – Tom Wells Jul 06 '10 at 08:27
  • 4
    "Like"ing (through iframe or fbml) is not an application so no additional permissions are required, all that is required is that you are logged into facebook. I'm still looking for a way to have a custom Like button not in an facebook hosted iframe. – Luke Jul 13 '10 at 11:23
  • Luke - me too! Let us know if you find a way to have a custom button (Android API in my case) to like a Page or an App. – Andrew Mackenzie Jul 24 '11 at 18:44
0

You can like an object with its object id using Facebook api using the following piece of code

[FBRequestConnection startForPostWithGraphPath:[NSString stringWithFormat:@"/%@/likes",{object_id}] graphObject:nil completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
   //do you customisation post like here
}];
Avinash
  • 4,304
  • 1
  • 23
  • 18