Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook curl like post request using open graph

I want to create the like button in my iOS app by using

curl -X POST \
  -F 'access_token=USER_ACCESS_TOKEN' \
  -F 'object=OG_OBJECT_URL' \
  https://graph.facebook.com/[User FB ID]/og

.likes given by Open graph api.

For it I have made post request i.e.

  NSString *stringurl=[NSString stringWithFormat:@"{access_token=AAAE08AxGmwghghghOWAxMZBz6jZAaRTUYTRMakJln3VdAeUju6E6tFn2I9feY5wtGatTm2KIgXoXYFZB0pTPQt2Oj0sNaRuX3AZDZD}&{object=http://www.google.com}&https://graph.facebook.com/100002097766610/og.likes"];

NSURL *url = [NSURL URLWithString: stringurl];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];

[request1 setURL:url];
[request1 setHTTPMethod:@"POST"];
[request1 setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string..:%@",string);

But I am getting empty string. May be I am making wrong url string. If any one know that how to make the curl post request. Please give me some idea.

Thanks to all.

like image 297
Sudha Tiwari Avatar asked Oct 21 '22 13:10

Sudha Tiwari


1 Answers

I attached some codes below:

Body data of Http POST request should located in 'Body', So I used 'setHttpBody:' method.

NSString *userId = @"Your ID Facebook ID";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/og.likes", userId]];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:@"POST"];
NSString *body=[NSString stringWithFormat:@"access_token=YOUR_APP_ACCESS_TOKEN&object=OG_OBJECT_URL"];
[request1 setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string..:%@",string);
like image 145
Sung-Pil Lim Avatar answered Oct 27 '22 09:10

Sung-Pil Lim