我正在尝试将图像上传到 twitter api。我正在使用 PlainOAuth。这是我所拥有的:
oauth_token = accessToken;
oauth_token_secret = accessTokenSecret;
OAuth *oAuth = [[OAuth alloc] initWithConsumerKey:***** andConsumerSecret:****];
oAuth.oauth_token = accessToken;
oAuth.oauth_token_secret = accessTokenSecret;
oAuth.oauth_token_authorized = YES;
[oAuth release];
NSString *url = @"https://api.twitter.com/1.1/statuses/update_with_media.json";
NSArray *params = [NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@=%@", @"status", [tweetStatus encodedURLParameterString]],
imageData, @"media[]",
nil];
NSDictionary *paramsDict = [NSDictionary dictionaryWithObjectsAndKeys:
imageData, @"media[]",
tweetStatus, @"status",
[accessToken stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], @"oauth_token",
nil];
NSString *oauth_header = [super oAuthHeaderForMethod:@"POST" andUrl:url andParams:paramsDict andTokenSecret:accessTokenSecret];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0f];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [[NSMutableData alloc] initWithData:[[params componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"media[]\"; filename=\"media.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:body];
[request addValue:oauth_header forHTTPHeaderField:@"Authorization"];
NSHTTPURLResponse *response;
NSError *error = nil;
NSString *responseString = [[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] encoding:NSUTF8StringEncoding] autorelease];
responseString
正在输出空白。所以我猜有404错误?如果我media[]
从我的任何一个参数数组中删除信息,我会收到一个 oAuth 错误。
请帮忙!