1

i want to like a post in Facebook, i have post_id , and i am not able to find the FQL Query for liking particular post from the Facebook developer page in IOS SDK.

From Facebook developer page, it says that you can like a post with the used of HTTP POST method it means we can't use GraphAPI or fql.query to like a post.

Can anyone please share HTTP POST URL to like a post in Facebook.

is anyone here who develop the like button functionality for Facebook post using custom button in iOS.

Thanks in advance.

Apple
  • 736
  • 1
  • 6
  • 24

3 Answers3

2

Here is an example if you are using Facebook SDK in iOS:

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/likes", post_id]
                             parameters:[NSDictionary dictionary]
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) 
                          {
                              if (error)
                              {
                                  NSLog(@"Error: %@", [error localizedDescription]);
                              }
                              else
                              {
                                  NSLog(@"Result: %@", result);           
                              }
                          }];
  • thanks for your reply but from this code i think this Graph request will return the total number of likes for particular post id ? am i right ? and ya in my Facebook SDK there is no any FBRequestConnection class. – Apple Feb 27 '13 at 14:12
  • No, notice that HTTPMethod parameter its set to POST, POST HTTP action will like the post on Facebook, if you change that to GET you will get the number of likes, if you change it to DELETE you will delete your existing already made like. The result in completion handler will be a NSDictionary object containing the response from Facebook which will be different depending on HTTPMethod you have chosen. The FBRequestConnection class must be there if you added the #import statement. And of course you must first initiate an FBSession for all this to work. – Ivo Patrick Tudor Weiss Feb 27 '13 at 14:43
1

I see you are asking for fields(parameters) for HTTP POST URL. An HTTP POST request does not (usually) contain parameters on which you are probably used to when you pass them in a classic GET request such as ?param1=value&param2=value after the script name in some URL.

POST request sends data to the server inside the message body, check out: http://en.wikipedia.org/wiki/POST_(HTTP)

Now that you know that, this is what you can do:

You CAN get the number of likes with a classic GET request, an URL that you can paste into any web browser and get the response, for example:

https://graph.facebook.com/260895413924000_605362559477282/likes?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

This url will give you a response with all the people who liked that post/photo.

You can leave out the ?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx if you know the post/photo is public as this one is (https://www.facebook.com/photo.php?fbid=605362542810617&set=a.260905783922963.82517.260895413924000). If it is not you need to generate one actual access_token(also for posting you NEED to generate one) and for testing you can do it here: https://developers.facebook.com/tools/explorer/

Now if you want to actually like the photo you can't simply form an URL that you can copy/paste inside your browser and which will trigger the like action. That's because browsers do not do POST requests, you need to do it trough code as Ivo Patrick Tudor Weiss suggested or eventually for testing purposes you can do it with curl utility from console like this:

curl --data "access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" https://graph.facebook.com/260895413924005362559477282/likes 

and you can undo the like with HTTP DELETE ... like this:

curl --data "access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -X DELETE https://graph.facebook.com/260895413924000_605362559477282/likes

- UPDATE, for additional questions made by OP in the comments:

It is of course possible to use ASIHTTPRequest to make GET, POST and DELETE HTTP requests. However I would not advise the use of that library for your case. One reason is that the author of ASIHTTPRequest has stopped working on the library, and the other reason is that Facebook SDK for iOS is a better choice since with it you have many other things already taken care for you. That being said here are the examples:

First type either one of these three combinations depending on what you want:

Get all people who liked the specific post:
(for simplicity I omitted the access_token here but you can append it to the URL if needed)

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/260895413924000_605362559477282/likes"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

Like the specific post yourself:

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/260895413924000_605362559477282/likes"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" dataUsingEncoding:NSUTF8StringEncoding]];
//[request setRequestMethod:@"POST"]; // <--- NOT NEEDED since it is the default if you previously called appendPostData

Unlike the post:

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/260895413924000_605362559477282/likes"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" dataUsingEncoding:NSUTF8StringEncoding]];
[request buildPostBody];
[request setRequestMethod:@"DELETE"];

Then execute the actual request:

[request startSynchronous];
NSString *response = [request responseString];
NSLog(@"Response: %@", response);

Remember synchronous request is OK for testing but your GUI is going to be unresponsive if you use it on the main thread in an actual app. Learn how to do an asynchronous request here: http://allseeing-i.com/ASIHTTPRequest/How-to-use

As for your iOS example. It would be too much to write all the code here. And you already got the answer from Ivo Patrick Tudor Weiss which is perfectly correct. The only thing that is missing is the boilerplate code that you need to have to authenticate on Facebook and establish an FBSession.

I would advise you to go over this material here: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/

Download the latest SDK which contains also the sample code, and follow the tutorial on Facebook web. Then when you get the basics configured, get back to the answer you got from Ivo.

Ivan Kovacevic
  • 1,322
  • 12
  • 30
  • Thanks ivan, its really useful post for me but can you please give me the example to like a post in iOS SDK, if possible then give me the link of that example so i can download that source. and is it possible to pass a HTTP URL to like a post using ASIHTTPRequest – Apple Feb 28 '13 at 05:06
  • Note also that you can do all of the above with NSURLConnection, without including any 3rd party libraries. Check this post for example: http://stackoverflow.com/questions/2071788/iphone-sending-post-with-nsurlconnection – Ivan Kovacevic Feb 28 '13 at 10:24
  • Hello Ivan, thanks a lot for your reply, i am able to like a particular post with the help of your code, but when i used the Delete post method then response is True but actual post would not be unlike. I will give your answer as a True answer and up vote also. Thanks again – Apple Feb 28 '13 at 10:28
  • I've checked now. It seems that for HTTP DELETE to work together with some message, which in this case is your access token, you have to additionally call [request buildPostBody];. I will edit my answer to include that – Ivan Kovacevic Feb 28 '13 at 10:45
  • Hello, its completely solved now, for delete i used one more statement , that is : [request buildPostBody]; before setRequestMethod. – Apple Feb 28 '13 at 10:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25280/discussion-between-tapan-nathvani-and-ivan-kovacevic) – Apple Feb 28 '13 at 11:01
0

You can use Graph API to post a like to Facebook post. As it said in documentation here: http://developers.facebook.com/docs/reference/api/post/

To create a like you need to issue a HTTP POST request to the POST_ID/likes connection with the publish_stream permission. You can suppress the notification created when liking a Post by passing a notify parameter with value of false.

user15
  • 1,044
  • 10
  • 20
  • can you please give me the example of that HTTP POST URL to like a post ? because i don't know the fields which are passing in that URL – Apple Feb 27 '13 at 11:51