gpt4 book ai didi

ios - 通过 iOS App 在 Twitter 上分享视频

转载 作者:可可西里 更新时间:2023-11-01 04:22:03 33 4
gpt4 key购买 nike

是否可以使用 SLRequest 分享视频?

我可以使用相同的方式分享图片

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 !");
}
}];

最佳答案

我一直在通读 Twitter 视频上传 API 文档,它非常简单。您基本上需要向他们的 API 发出 3 个 POST 请求。您上传的视频也限制在 15 MB 以内。

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:发送带有视频大小(以字节为单位)的初始请求。这将返回我们必须在请求 2 和 3 中使用的媒体 ID 号。

  • 请求 2:使用请求 1 返回的媒体 ID 号上传视频数据。

  • 请求 3:视频上传完成后,将“FINALIZE”请求发送回 Twitter API。这让 Twitter API 知道视频文件的所有 block 都已完成上传。

注意 Twitter API 接受“ block ”中的视频上传。因此,如果您的视频文件很大,您可能希望将其拆分为多个文件,因此您将不得不多次重复“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

在回答您的第一条评论时,错误 503 表示 Twitter 服务器过载,现在无法处理您的请求。

503 Service Unavailable The Twitter servers are up, but overloaded with requests. Try again later.

关于ios - 通过 iOS App 在 Twitter 上分享视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612386/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com