Twitter API 1.1 - update_with_media返回500

时间:2014-02-03 00:56:13

标签: objective-c image twitter upload

我正在尝试使用REST API 1.1(网址:https://api.twitter.com/1.1/statuses/update_with_media.json)从Mac应用上传Twitter上的图片,但我总是得到错误代码500和{“错误”:[{“message” :“内部错误”,“代码”:131}]}。 如果我只上传一条推文(使用/update.json),它每次都可以正常工作。

这是我的代码:

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
[request setHTTPShouldHandleCookies:NO];

NSString *boundary = @"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

[self signRequest:request];  // Adding Oauth

NSMutableData *body = [NSMutableData dataWithLength:0];

// Adding tweet string
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSString stringWithFormat:@"%@\r\n",@"Uploading again test3"]dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",@"status"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];

// Adding image
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"media[]\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Set HTTPBody
[request setValue:@(body.length).stringValue forHTTPHeaderField:@"Content-Length"];
request.HTTPBody = body;

1 个答案:

答案 0 :(得分:0)

我对以下代码感到满意:

- (void)postTweet:(NSString *)tweetString withImageData:(NSData *)imageData {

NSURL *baseURL = [NSURL URLWithString:url_statuses_update_with_media];

NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"status"] = tweetString;
params[@"media[]"] = imageData;

[self sendPOSTRequestForURL:baseURL andParams:params];}                                    

-(NSError *)sendPOSTRequestForURL:(NSURL *)url andParams:(NSDictionary *)params { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f]; [request setHTTPMethod:@"POST"]; [request setHTTPShouldHandleCookies:NO];

NSString *boundary = @"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

[self signRequest:request];

NSMutableData *body = [NSMutableData dataWithLength:0];

for (NSString *key in params.allKeys) {
    id obj = params[key];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSData *data = nil;

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];

    if ([obj isKindOfClass:[NSData class]]) {
        [body appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        data = (NSData *)obj;
    } else if ([obj isKindOfClass:[NSString class]]) {

        data = [[NSString stringWithFormat:@"%@",(NSString *)obj]dataUsingEncoding:NSUTF8StringEncoding];
    }

    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:data];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setValue:@(body.length).stringValue forHTTPHeaderField:@"Content-Length"];
request.HTTPBody = body;

NSURL *baseURL = [NSURL URLWithString:url_statuses_update_with_media]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"status"] = tweetString; params[@"media[]"] = imageData; [self sendPOSTRequestForURL:baseURL andParams:params];}