Uploading chunked video FINALIZE 400ing

media-upload

#1

I am uploading a video via Twitter Kit on iOS in the following way:

class Dummy {
    static func uploadToTwitter(userID: String, title: String, description: String, videoURL: URL) {
        do {
            let data = try Data(contentsOf: videoURL)
            let client = TWTRAPIClient(userID: userID)
            startRequest(data: data, client: client)
        } catch let error {
            print(error)
        }
    }
    
    static func startRequest(data: Data, client: TWTRAPIClient) {
        let params: [String : Any] = [
            "media_category": "tweet_video",
            "command": "INIT",
            "media_type": "video/mp4",
            "total_bytes": "\(data.count)"]
        
        var initError: NSError?
        let request = client.urlRequest(
            withMethod: "POST",
            url: "https://upload.twitter.com/1.1/media/upload.json",
            parameters: params,
            error: &initError)
        
        client.sendTwitterRequest(request, completion: { (response, data, error) in
            if let error = error {
                print(error)
            } else if let data = data,
                let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any],
                let mediaID = json["media_id_string"] as? String {
                
                self.append(mediaID: mediaID, withVideo: data, client: client)
            }
        })
        
        if let error = initError {
            BPLogError(error)
        }
    }
    
    static func append(mediaID: String, withVideo video: Data, client: TWTRAPIClient) {
        let videoStr = (video as NSData).base64EncodedString(options: [])
        let params: [String : Any] = [
            "command": "APPEND",
            "media_id": mediaID,
            "segment_index": "\(0)",
            "media_data": videoStr
        ]
        var appendError: NSError?
        let request = client.urlRequest(
            withMethod: "POST",
            url: "https://upload.twitter.com/1.1/media/upload.json",
            parameters: params,
            error: &appendError)
        
        client.sendTwitterRequest(request) { (response, data, error) in
            if let error = error {
                print(error)
            } else {
                self.finalize(mediaID: mediaID, client: client)
            }
        }
        
        if let error = appendError {
            BPLogError(error)
        }
    }
    
    static func finalize(mediaID: String, client: TWTRAPIClient) {
        let params: [String : Any] = [
            "command": "FINALIZE",
            "media_id": mediaID
        ]
        var finalizeError: NSError?
        let request = client.urlRequest(
            withMethod: "POST",
            url: "https://upload.twitter.com/1.1/media/upload.json",
            parameters: params,
            error: &finalizeError)
        client.sendTwitterRequest(request) { (response, data, error) in
            print(error)
            /**
 Error is:
 Error Domain=TWTRNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo={NSLocalizedFailureReason=, TWTRNetworkingStatusCode=400, NSErrorFailingURLKey=https://upload.twitter.com/1.1/media/upload.json, NSLocalizedDescription=Request failed: bad request (400)}
 Response is nil
 Data is nil
 */

        }
        
        if let error = finalizeError {
            BPLogError(error)
        }
        
    }
}

And the FINALIZE request is sending me a 400 with no response while all other responses are 200 or 204. The video is 1.4mb.

An example media ID that failed in this way is 910264877570195457

Any suggestions for what I should be doing to fix this?


#2

update: i am an idiot & nothing to see here :slight_smile:


#3

What was your solution?