'Like' a page using Facebook Graph API
Asked Answered
P

6

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?

Phrasing answered 17/6, 2010 at 11:3 Comment(0)
T
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.

Tracee answered 20/6, 2012 at 19:24 Comment(5)
I dont think you can like a page using the built-in like cation. Can you confirm?Traceetracer
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."?Construction
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."Construction
@BK. Does this actually mean that there is no other way of liking a facebook page than using the like button plugin?Grisgris
How can I find out the OG_OBJECT_URL from a page that would I like?Expectancy
P
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.

Pow answered 7/3, 2014 at 14:1 Comment(0)
P
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.

Persistence answered 17/10, 2011 at 18:22 Comment(1)
I think the OP is asking how to do this programatically, perhaps as a way of passing a "like" through a custom control.Caddric
S
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/

Subjectivism answered 24/6, 2012 at 12:34 Comment(1)
As mentioned in the docs and in BK's answer, this doesn't work for Facebook Pages, only for other objectsChappy
M
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

Monogenic answered 17/6, 2010 at 11:21 Comment(3)
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).Phrasing
"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.Monogenic
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.Ruckus
A
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
}];
Aretina answered 19/3, 2014 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.