我尝试使用SLRequest以编程方式将图片(从iOS设备)上传到用户的Twitter帐户。 Tweet文本已上传到推文中,但图片永远不会出现。响应状态代码为200。
显然基本工作正常,否则推文根本不会显示在用户的流中,我已经检查过找到了图像,并且(NSData)变量包含了预期的字节数。 / p>
查看以下代码 - 正如您可以看到注释掉的代码,我尝试了各种不同的修改但没有成功...任何想法/帮助表示赞赏!
- (void) postToTwitter:(NSString *)postMessage withImageNamed:(NSString *)imageName {
// Get Twitter account
ACAccountStore *smAccount = [[ACAccountStore alloc] init];
ACAccountType *smType = [smAccount accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSDictionary *postOptions = nil;
// Attempt to post the postMessage
[smAccount requestAccessToAccountsWithType:smType options:postOptions completion:^(BOOL accessGranted, NSError *error) {
// Access code block
if (accessGranted) {
NSLog(@"Access permitted");
NSArray *smAccountList = [smAccount accountsWithAccountType:smType];
if ([smAccountList count] > 0) {
// Get the first Twitter account
ACAccount *postingAccount = [smAccountList lastObject];
// Create the post request
//SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:postMessage forKey:@"status"]];
//SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://upload.twitter.com/1.1/media/upload.json"] parameters:nil];
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:nil];
// Add image to post
// TODO - Check which to use >
UIImage *postImage = [UIImage imageNamed:imageName];
//UIImage *postImage = [UIImage imageWithContentsOfFile:imageName];
//NSData *imageData = UIImageJPEGRepresentation (postImage, 0.5f); // Set compression
NSData *imageData = UIImagePNGRepresentation(postImage);
//[postRequest addMultipartData:imageData withName:@"media" type:@"image/png" filename:@"image.png"];
[postRequest addMultipartData:imageData withName:@"media[]" type:@"multipart/form-data" filename:@"image.png"];
// Add text
[postRequest addMultipartData:[postMessage dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data" filename:nil];
// Execute the post
[postRequest setAccount:postingAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// Post code block
if (error) {
NSLog(@"Post failed - %@", error);
if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)])
[_delegate didNotPost:self];
} else {
NSLog(@"Post complete");
if (_delegate && [_delegate respondsToSelector:@selector(didPost:)])
[_delegate didPost:self];
}
}];
}
} else {
NSLog(@"Access denied - %@", error);
if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)])
[_delegate didNotPost:self];
}
} ];
}
这就是回应:
(lldb) po urlResponse
<NSHTTPURLResponse: 0x7fb9bf80> { URL: https://api.twitter.com/1.1/statuses/update.json } { status code: 200, headers {
"Cache-Control" = "no-cache, no-store, must-revalidate, pre-check=0, post-check=0";
"Content-Disposition" = "attachment; filename=json.json";
"Content-Encoding" = gzip;
"Content-Length" = 751;
"Content-Type" = "application/json;charset=utf-8";
Date = "Thu, 02 Apr 2015 17:14:20 GMT";
Expires = "Tue, 31 Mar 1981 05:00:00 GMT";
"Last-Modified" = "Thu, 02 Apr 2015 17:14:20 GMT";
Pragma = "no-cache";
Server = "tsa_b";
"Set-Cookie" = "lang=en";
Status = "200 OK";
"Strict-Transport-Security" = "max-age=631138519";
"x-access-level" = "read-write";
"x-connection-hash" = 6f61f3c96ba990931cca3fa950a42b38;
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-response-time" = 51;
"x-transaction" = b2c77803db7e996d;
"x-tsa-request-body-time" = 426;
"x-twitter-response-tags" = BouncerCompliant;
"x-xss-protection" = "1; mode=block";
} }
答案 0 :(得分:4)
在我发布此消息时,不推荐使用update_with_media。虽然它现在可以使用,但它可能在未来突然停止 - 这可能会使您的应用程序在将来不再工作。
在阅读了Twitter的API文档之后,我设法使其无需使用&#34; update_with_media&#34;。
上传图片(或视频)所涉及的过程是:
使用/media/upload.json SLRequest首先上传媒体(图片或视频)。使用nil参数并使用添加图像 &#34; addMultipartData&#34; SLRequest的方法。
在SLRequest上调用performRequestWithHandler。
等待来自Twitter的回复以获取&#34;媒体ID&#34;从responseData(将数据转换为JSON格式)并检索 &#34; media_id_string&#34;进入NSString。
- 醇>
使用/statuses/update.json启动另一个SLRequest,并将Media ID(key:media_ids)附加到参数字典中 使用状态文本(键:状态)。
NSDictionary *parameters = @{@"status": @"See my image:",
@"media_ids": mediaID };
- 在第二个SLRequest上调用performRequestWithHandler以提交推文。
醇>
有关详细代码,请参阅我的教程博客:http://xcodenoobies.blogspot.my/2016/01/how-to-using-slrequest-to-upload-image.html
答案 1 :(得分:2)
经过多次痛苦,我意识到我发布了错误的Twitter API。以下修改使这项工作成功。
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"] parameters:nil];