0

我想通过从我的应用程序发送 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 [第 432 行] JSON: { error = { code = 100; message = "(#100) 参数对象是必需的"; 类型 = OAuthException; }; }

我也在使用这种方法:

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setParameterEncoding:AFJSONParameterEncoding];
[self setDefaultHeader:@"Accept" value:@"application/json"];

所以 exampleObject NSDictionary 应该自动转换为 JSON

编辑

此 curl 命令工作正常:

curl -X POST " https://graph.facebook.com/me/objects/myapp:comments " -F "access_token=CAAH3rJC7RHUBAHP6NHGaa5Pr2MKlJT4X9GZBZBkc93T9TNHqoSV411eD2uvOw2XInpZCZBryY4V6OtvHP5xMDZCJfyiieTe8IB1lS68gYkNlKj6bNYfcsPZCGpamC2wWVJDkAYm9hVHI3fASDGxbHKWUQYam8JZAwblA68y8HXZA1v2EaDZAe3VabyJQ37Q5rf6JVpWiRV50dKH9XSa7I7ZAcg9QajPhWnk3PEZD" -F "object={\"title\":\"Titleeeee\"}"

4

1 回答 1

0

好的,我使用了不同的方法并且它有效,这是我的代码,也许有人不会像我那样浪费时间:

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

它适用于不同的图形对象(网站),但这不是问题

于 2013-08-01T22:17:14.947 回答