我正在尝试将图片上传到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错误。
请帮忙!
答案 0 :(得分:1)
TWTweetComposeViewController *twitterViewController = [[TWTweetComposeViewController alloc] init];
NSString *twitterText = [NSString stringWithFormat:@"%@%@%@", kBookingShareTwitterText, kBookingShareFlightURL, bookingId];
[twitterViewController setInitialText:twitterText];
[twitterViewController addImage:[UIImage imageNamed:@"logo.png"]];
[self presentModalViewController:twitterViewController animated:YES];
[twitterViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
[self dismissModalViewControllerAnimated:YES];
break;
case SLComposeViewControllerResultDone:
[self updateBookingWithFriendsInvitationFlag];
break;
default:
break;
}
}];