我可以使用相同的方式共享图像
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message]; if (isImage) { NSData *data = UIImagePNGRepresentation(imgSelected); [postRequest addMultipartData:data withName:@"media" type:@"image/png" filename:@"TestImage.png"]; } postRequest.account = account; [postRequest performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error) { if (!error) { NSLog(@"Upload Sucess !"); } }];
解决方法
Uploads using this endpoint require at least 3 calls,one to
initialize the request,which returns the media_id,one or more calls
to append/upload binary or base64 encoded data,and one last call to
finalize the upload and make the media_id usable with other resources.
它的工作原理如下:
>请求1:发送具有视频大小(以字节为单位)的init请求.这将返回我们必须在请求2和3中使用的媒体ID号.
>请求2:使用请求1中返回的媒体ID号上传视频数据.
>请求3:视频上传完成后,将“FINALIZE”请求发送回Twitter API.这让Twitter API知道视频文件的所有块都已完成上传.
注意Twitter API接受“块”中的视频上传.因此,如果您的视频文件很大,您可能希望将其拆分为多个文件,因此您必须多次重复“请求2”(不要忘记每次都增加“segment_index”数字).
我已经开始编写以下代码了.尝试一下并尝试一下.我稍后会更新我的答案以改进它.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Assign the mediatype to a string NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; // Check the media type string so we can determine if its a video if ([mediaType isEqualToString:@"public.movie"]) { NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; NSData *webData = [NSData dataWithContentsOfURL:videoURL]; // Get the size of the file in bytes. NSString *yourPath = [NSString stringWithFormat:@"%",videoURL]; NSFileManager *man = [NSFileManager defaultManager]; NSDictionary *attrs = [man attributesOfItemAtPath:yourPath error: NULL]; UInt32 result = [attrs fileSize]; //[self tweetVideoStage1:webData :result]; [self tweetVideo:webData :result :1 :@"n/a"]; } } -(void)tweetVideo:(NSData *)videoData :(int)videoSize :(int)mode :(NSString *)mediaID { NSURL *twitterVideo = [NSURL URLWithString:@"https://upload.twitter.com/1.1/media/upload.json"]; // Set the parameters for the first twitter video request. NSDictionary *postDict; if (mode == 1) { postDict = @{@"command": @"INIT",@"total_bytes" : videoSize,@"media_type" : @"video/mp4"}; } else if (mode == 2) { postDict = @{@"command": @"APPEND",@"media_id" : mediaID,@"segment_index" : @"0",@"media" : videoData }; } else if (mode == 3) { postDict = @{@"command": @"FINALIZE",@"media_id" : mediaID }; } SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL:twitterVideo parameters:postDict]; // Set the account and begin the request. postRequest.account = account; [postRequest performRequestWithHandler:^(NSData *responseData,NSError *error) { if (!error) { if (mode == 1) { // Parse the returned data for the JSON string // which contains the media upload ID. NSMutableDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error] NSString *tweetID = [NSString stringWithFormat:@"%@",[returnedData valueForKey:@"media_id_string"]]; [self tweetVideo:videoData :result :2 :tweetID]; } else if (mode == 2) { [self tweetVideo:videoData :result :3 :mediaID]; } } else { NSLog(@"Error stage %d - %",mode,error); } }]; }
更新 – Twitter API错误 – https://dev.twitter.com/overview/api/response-codes
在回答您的第一条评论时,错误503表示Twitter服务器过载,无法立即处理您的请求.
503 Service Unavailable The Twitter servers are up,but overloaded with requests. Try again later.