通过iOS App在Twitter上分享视频

通过iOS App在Twitter上分享视频,第1张

概述是否可以使用SLRequest共享视频? 我可以使用相同的方式共享图像 SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message];if (isImage){ 是否可以使用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 !");    }}];@H_301_5@解决方法 我一直在阅读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:发送具有视频大小(以字节为单位)的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);        }    }];}@H_301_5@  

更新 – 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 App在Twitter上分享视频全部内容,希望文章能够帮你解决通过iOS App在Twitter上分享视频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存