【问题标题】:Posting image to Twitter API将图像发布到 Twitter API
【发布时间】:2013-01-15 04:18:16
【问题描述】:

我正在尝试将图像上传到 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 错误。

请帮忙!

【问题讨论】:

  • 你可以使用 twitter 框架代替 API
  • @NAZIK 是的,但是对于其他一切我都使用了 API...所以我不想仅仅为了这个小功能将他们所有的文件导入我的项目。
  • 您的应用的部署目标是什么?

标签: objective-c oauth twitter


【解决方案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;
    }


}];

【讨论】:

  • 这不是我想要的。我正在与多个社交网络合作,这不会被简化。我希望它在后台发布图片,而不会出现任何 Twitter 视图控制器。
  • 另外,TWTweetComposeViewController 在 iOS 6 中已被弃用。
猜你喜欢
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 2015-04-24
  • 2015-09-03
  • 1970-01-01
  • 2020-03-14
  • 2013-06-10
相关资源
最近更新 更多