我一直在阅读 Twitter 视频上传 API 文档,它确实非常简单。您基本上需要向他们的 API 发出 3 个 POST 请求。您上传的视频大小也限制为 15 MB。
使用此端点上传至少需要 3 次调用,其中一次调用
初始化请求,返回media_id,一次或多次调用
附加/上传二进制或 Base64 编码数据,最后一次调用
完成上传并使 media_id 可与其他资源一起使用。
所以它的工作原理是这样的:
请求 1:发送带有视频大小(以字节为单位)的初始化请求。这将返回我们必须在请求 2 和 3 中使用的媒体 ID 号。
请求2:使用请求1返回的Media ID号上传视频数据。
请求 3:视频上传完成后,将“FINALIZE”请求发送回 Twitter API。这让 Twitter API 知道视频文件的所有块都已完成上传。
NoteTwitter API 接受“块”视频上传。因此,如果您的视频文件相当大,您可能希望将其拆分为多个文件,因此您将不得不多次重复“Request 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, NSHTTPURLResponse *urlResponse, 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 https://dev.twitter.com/overview/api/response-codes
在回答您的第一条评论时,错误 503 意味着 Twitter 服务器超载,目前无法处理您的请求。
503 服务不可用 Twitter 服务器已启动,但过载
与请求。稍后再试。