2

我的 URL 是正确的,但我的请求必须是格式错误的。我得到了 400。有什么想法吗?API 文档说,如果传递参数而不是 JSON 正文,它可能会返回 400。提前致谢。

- (void)postFeedItem:(NSDictionary *)paramDict Response:(void (^)(id))callbackBlock Failure:(void (^)())failure {

NSString *targetUrl = [NSString stringWithFormat:@"%@/services/data/v23.0/chatter/feeds/user-profile/%@/", _appManager.coordinator.credentials.instanceUrl, [_appManager.userInformation objectForKey:@"sfUserId"]];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:targetUrl]];
/*
 { "body" :
 {
 Request body example:
 "messageSegments" : [
 {
 "type": "Text",
 "text" : "New post"
 }
 ]
 }
 }
 */

NSArray *arr = [NSArray arrayWithObjects:
                [NSDictionary dictionaryWithObjectsAndKeys:@"Text", @"type",
                 @"New post", @"text",
                 nil],
                nil];
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSDictionary dictionaryWithObjectsAndKeys:arr, @"messageSegments", nil],
                      @"body",
                      nil];

NSLog(@"JSON: %@", [[NSString alloc] initWithData:[self toJSON:info] encoding:NSUTF8StringEncoding]);

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"feed-items" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendData:[self toJSON:info]];
}];

[request setValue:[NSString stringWithFormat:@"OAuth %@",_appManager.coordinator.credentials.accessToken] forHTTPHeaderField:@"Authorization"];
[request addValue:@"false" forHTTPHeaderField:@"X-Chatter-Entity-Encoding"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"success");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"FAILED CHATTER Request: %@ - %@ - %@", [request URL], [request allHTTPHeaderFields], error.description);
}];

[operation start];

}

这是 NSLog:

2012-09-19 18:10:54.732 RingDNA Free[6018:c07] __47-[ChatterHelper postFeedItem:Response:Failure:]_block_invoke_087 [Line 76] FAILED CHATTER Request: https://na4.salesforce.com/services/data /v23.0/chatter/feeds/user-profile/00560000001j3paAAA/feed-items- { "接受编码" = gzip; "接受语言" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr , uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8"; 授权=“OAuth 00D60000000KV29!ARkAQFWhnhOtcGFgVMT4MkZHCV3zG9SY4en66718BiG_ZY59W0gR1iSWA8i.ey_b94vqjRW_RQITALBWmfpPrKTGk”;“内容类型”=“多部分/表单数据;边界=边界+0xAbCdEfGbOuNdArY,应用程序/json”;“用户代理”=“com.ringdna.dreamforce.RingDNA-Free/36(未知,iPhone OS 5.1,iPad 模拟器,Scale/1.000000)”;“X-Chatter-Entity-Encoding”=假;} - 错误域=com.alamofire.networking.error Code=-1011 “预期状态代码 [索引数:100(在 1 个范围内),索引:(200-299)],

4

1 回答 1

0

经过几次尝试,我们发现了这一点。基本上,SFDC 会验证您发送到 Chatter API 的参数的顺序,它会出现。如果这对其他人有帮助,现在可以在 Github 中使用 Chatter 帖子的代码示例:

https://github.com/kyleroche/Dreamforce-2012

于 2012-11-10T17:57:12.193 回答