iphone - Facebook curl like 使用开放图的发布请求

标签 iphone ios objective-c facebook-graph-api curl

我想在我的 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 给出。

为此,我提出了 post 请求,即

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

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

感谢大家。

最佳答案

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

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

关于iphone - Facebook curl like 使用开放图的发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739559/

相关文章:

ios - 在我的网站上统计我的应用下载量

objective-c - undefined symbol : _OBJC_CLASS_$_FIRApp

iphone - 如何使用 UIScrollview 动态加载图像

ios - 应用程序在设备上崩溃但可以在模拟器 iOS 上运行

iphone - 具有多个 UIViewController 的 UIScrollView

ios - DidReceiveNotificationRequest 未被调用

ios - 在 Storyboard中设计 UICollectionViewCell

ios - Collectionview 单元格之间的最大间距

ios - 单击按钮展开和折叠第一行 Objective - C

ios - UICollectionView 显示两行,但我只想要一行