5

我想iOS通过使用在我的应用程序中创建类似按钮

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

.likes 由Open graph api.

为此,我提出了发布请求,即

  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);

但我得到空字符串。可能是我制作了错误的 url 字符串。如果有人知道如何提出 curl post 请求。请给我一些想法。

谢谢大家。

4

1 回答 1

3

我在下面附上了一些代码:

Http POST 请求的正文数据应该位于“Body”中,所以我使用了“setHttpBody:”方法。

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);
于 2013-04-05T10:09:02.220 回答