iOS Swift:如何使用TwitterKit将video发布到Twitter?

我没有find相关的和最新的答案。

这是我目前正在使用的代码,所有的请求处理正确,但没有video张贴?

if let userID = Twitter.sharedInstance().sessionStore.session()?.userID { var client = TWTRAPIClient(userID: userID) let text: String = "Testing Video" let videoLength: String = "\(self.video.length)" print(videoLength) var initError: NSError? var message = ["status": text, "command" : "INIT", "media_type" : "video/m4v", "total_bytes" : videoLength] let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &initError) client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in if error == nil { do { let json: NSDictionary = try (NSJSONSerialization.JSONObjectWithData(responseData!, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary)! print("JSON is \(json)") let mediaID = json.objectForKey("media_id_string") as! String client = TWTRAPIClient(userID: userID) var uploadError: NSError? let videoString = self.video.base64EncodedStringWithOptions([]) message = ["command" : "APPEND", "media_id" : mediaID, "segment_index" : "0", "media" : videoString] let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &uploadError) client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in if error == nil { client = TWTRAPIClient(userID: userID) var finalizeError: NSError? message = ["command":"FINALIZE", "media_id": mediaID] let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &finalizeError) client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in if error == nil { client = TWTRAPIClient(userID: userID) var sendError: NSError? let message = ["status": text, "wrap_links": "true", "media_ids": mediaID] //var updateMessage = NSMutableDictionary(dictionary: message) let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strStatusUrl, parameters: message , error: &sendError) client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in }) } else { print("Command FINALIZE failed \n \(error!)") } }) } else { print("Command APPEND failed") } }) } catch { print("\(error)") } } else { print("\(error.debugDescription)Command INIT failed") } }) } 

以上所有的代码正在工作,除了video没有上传。 我无法弄清楚我错过了什么,Twitter的文档在发布video时很差。

我同情你关于Twitter的糟糕文件。 找出你得到的错误。

下面是我希望它可以帮助的实现说明:

  1. Twittervideo要求: https : //dev.twitter.com/rest/public/uploading-media#videorecs 。
  2. 在完成上传之前,FINALIZE命令会validation每个Twittervideo要求的video文件。
  3. 如果您收到“无效或不支持的媒体,原因:不支持的媒体”响应数据“HTTP状态400错误请求”。 发送FINALIZE命令后出错,您需要validation您的video文件与Twittervideo要求。

看看我的项目https://github.com/mtrung/TwitterVideoUpload 。 我知道这是Obj-C,但它的工作原理。