我想通过从我的应用程序发送http请求来创建Facebook开放图对象,我不使用Facebook SDK,我使用的是社交网络框架,并通过AFNetworking构建打开图的请求。我的代码:
- (void)didInitializeSocialAccountStore:(BOOL)succes forPermissions:(AccountStorePermissions)permissionsType
{
NSURLRequest *request = [self requestWithMethod:@"POST" path:[NSString stringWithFormat:@"https://graph.facebook.com/me/objects/myapp:comments?access_token=%@", [MGSocialNetworkSingleton sharedInstance].facebookAccesToken] parameters:self.exampleObject];
if (permissionsType == AccountStorePermissions_Publish)
{
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
DLog(@"response: %@n", response);
DLog(@"JSON: %@n", JSON);
DLog(@"URL: %@n", request.URL);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
DLog(@"response: %@n", response);
DLog(@"error: %@n", error);
DLog(@"JSON: %@n", JSON);
DLog(@"URL: %@n", response.URL);
}];
[operation start];
}
}
- (NSDictionary *)exampleObject
{
return @{@"object":@{@"title":@"Titleeeee"}};
}
请求发出后,我得到了以下错误响应:
didInitializeSocialAccountStore:forPermissions:]_block_invoke_2[Line 432]JSON:{错误={代码=100;message="(#100)参数对象是必需的";type=OAuthException;};}
我也在使用这种方法:
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setParameterEncoding:AFJSONParameterEncoding];
[self setDefaultHeader:@"Accept" value:@"application/json"];
因此exampleObject NSDictionary应该自动转换为JSON
编辑
这个curl命令运行良好:
卷曲-X POST"https://graph.facebook.com/me/objects/myapp:comments"-F"access_token=CAAH3rJC7RHUBAHP6NHGaa5Pr2MKlJT4X9GZBZBkc93T9TNHqoSV411eD2uvOw2XInpZCZBryY4V6OtvHP5xMDZCJfyiieTe8IB1lS68gYkNlKj6bNYfcsPZCGpamC2wWVJDkAYm9hVHI3FASDGxbHKWUQAm8JZAwblA68y8HXZA1v2EaDZAe3VabyJQ37Q5rf6JVpWiRV50dKH9XSa7I7ZAcg9QajPhWnk3PEZ D"-F"object={\"title \":\"Titleeee \"}"
好吧,我使用了不同的方法,它很有效,这是我的代码,也许有人不会像我那样浪费时间:
NSDictionary *parameters = @{@"title":@"work you f****r"};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:[[NSString stringWithFormat:@"%@/me/objects/website?access_token=%@&object=%@", kMGFacebookAPIBaseURLString, [MGSocialNetworkSingleton sharedInstance].facebookAccesToken, jsonString] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] parameters:nil];
它返回:
response: {
id = 709840849042702;
}
它适用于不同的图形对象(网站),但在这里不是问题