2

I haven't found relevant and up to date answer on SO.

Here is the code I'm currently using, all requests are handled correctly but no video is posted ?

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")
        }
    })
    }

All the code above is working, except that the video is not uploaded. I can't figure out what I'm missing and Twitter's documentation is very poor on posting video.

0

2 Answers 2

3

If you want to convert @Trung's code to Swift 4, and if you want to USE TwitterKit, you can use this enum to handle all upload stages:

enum UploadStage {
    case initial(size: String, videoDuration: Int?) // if your video duration is <= 30s, you can pass nil here
    case append(mediaId: String, videoData: Data, segment: Int)
    case finalize(mediaId: String)
    case status(status: String, mediaId: String)

    static let videoChunkMaxSize = 5 * 1000 * 1000

    var parameters: [String: Any] {
        get {
            switch self {

            case .initial(let size, let videoDuration):
                var params = ["command":stageName, "total_bytes": size, "media_type": "video/mp4"]
                if let videoDuration = videoDuration, videoDuration > 30 {
                    params["media_category"] = "tweet_video"
                }
                return params
            case .append(let mediaId, _ , let segment):
                let videoChunkString = self.videoChunk!.base64EncodedString(options: [])
                return ["command":stageName, "media_id": mediaId, "segment_index": "\(segment)", "media": videoChunkString]
            case .finalize(let mediaId):
                return ["command":stageName, "media_id": mediaId]
            case .status(let status, let mediaId):
                return ["status": status, "wrap_links": "true", "media_ids": mediaId]
            }
        }
    }

    var stageName: String {
        get {
            switch self {
            case .initial:
                return "INIT"
            case .append:
                return "APPEND"
            case .finalize:
                return "FINALIZE"
            case .status:
                return "STATUS"

            }
        }
    }

    var videoChunk: Data? {
        switch self {
        case .append(_ , let videoData, let segment):
            if videoData.count > UploadStage.videoChunkMaxSize {
                let maxPos = segment * UploadStage.videoChunkMaxSize + UploadStage.videoChunkMaxSize
                let range: Range<Data.Index> = segment * UploadStage.videoChunkMaxSize..<(maxPos >= videoData.count ? videoData.count : maxPos)
                return videoData.subdata(in: range)

            }
            return videoData
        default:
            return nil
        }
    }

    var urlString: String {
        switch self {
        case .initial, .append, .finalize:
            return "https://upload.twitter.com/1.1/media/upload.json"
        case .status:
            return "https://api.twitter.com/1.1/statuses/update.json"
        }
    }
}

UploadStage enum can be used in the method with recursive calls by passing next stage enum value like this:

func uploadTwitterVideo(videoData: Data, status: String, stage: UploadStage, success: @escaping () -> Void, failure: @escaping (Error?) -> Void) {

    let client = TWTRAPIClient.withCurrentUser()

    var clientError: NSError?
    let urlRequest = client.urlRequest(withMethod: "POST", urlString: stage.urlString, parameters: stage.parameters, error: &clientError)
    if clientError == nil {
        client.sendTwitterRequest(urlRequest) { (urlResponse, responseData, connectionError) in

            guard connectionError == nil else {
                print("There was an error: \(connectionError!.localizedDescription)")
                failure(connectionError)
                return
            }

            self.handleError(urlResponse, failure: failure)
            if let data = responseData, let dataString = String(data: data, encoding: .utf8), let urlResponse = urlResponse {
                print("Twitter stage \(stage.stageName) URL response : \(urlResponse), response data: \(dataString)")


                var nextStage: UploadStage?
                do {
                    switch stage {
                    case .initial:
                        let returnedJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String:Any]
                        if let mediaId = returnedJSON["media_id_string"] as? String {
                            print("stage one success, mediaID -> \(mediaId)")
                            nextStage = .append(mediaId: mediaId, videoData:videoData, segment: 0)
                        }
                    case .append(let mediaId, let videoData, let segment):
                        if ((segment + 1) * UploadStage.videoChunkMaxSize < videoData.count) {
                            nextStage = .append(mediaId: mediaId, videoData: videoData, segment: segment + 1)
                        } else {
                            nextStage = .finalize(mediaId: mediaId)
                        }
                    case .finalize(let mediaId):
                        nextStage = .status(status: status, mediaId: mediaId)
                    case .status:
                        success()
                    }

                    if let nextStage = nextStage {
                        self.uploadTwitterVideo(videoData: data, status: status, stage: nextStage, success: success, failure: failure)
                    }
                } catch let error as NSError {
                    failure(error)
                }
            }
        }
    }
}
5
  • 1
    This is a superb implementation and worked great the first time I tried it. One thing, you need to pull your connectionError guard out of the if-let for the response data. I received errors on long videos but no response data, so the error never showed up until I pulled that error check out. I read in the Twitter docs that max video length is 30 for sync upload, but 140 seconds for async, yet I continue to get 400 errors (same error that the TWTRComposerViewController throws) when uploading videos longer than 30 seconds using this async method. Any ideas on how to get to that 140 second limit?
    – jbcaveman
    Commented Mar 9, 2018 at 6:08
  • 2
    Found it: the docs say "constraints may vary based on the media_category parameter", but finding guidance beyond that is hard. Basically, for videos >30 seconds, you must add "media_category": "tweet_video" to the INIT params, and then after the finalize stage, you've got to repeat the last stage request until it gives you a response that the video has finished processing. If it's not ready, you will get no response and a 400 error. Then you can complete the tweet by proceeding with the final stage. jwz.org/blog/2016/06/…
    – jbcaveman
    Commented Mar 9, 2018 at 8:24
  • @JoelCave, thanks for your remarks. I've updated my answer. Now connectionError guard is moved before if let. My example is for videos with duration <= 30s. I've added video duration param to init stage, but I didn't implement repeating after finalize stage yet, because I need to test it.
    – bojanb89
    Commented Apr 13, 2018 at 10:39
  • Value of type 'UploadStage' has no member 'urlString' Commented May 23, 2019 at 6:21
  • Thanks @ShahbazAkram. I've updated replaced url variable with urlString. It should work fine now.
    – bojanb89
    Commented May 24, 2019 at 7:44
2

I sympathize with you about about Twitter's poor documentation. Find out what error you get.

Below are implementation notes that I hope it helps:

  1. Twitter video requirement: https://dev.twitter.com/rest/public/uploading-media#videorecs.
  2. FINALIZE command verifies video file per Twitter video requirement before completing the upload.
  3. If you get "HTTP status 400 bad request" with response data having "Invalid or unsupported media, Reason: UnsupportedMedia." error after sending FINALIZE command, you need to verify your video file with Twitter video requirement.

Check out my project https://github.com/mtrung/TwitterVideoUpload. I know it's in Obj-C but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.