Swift - 如何上传视频到 youtube 不公开?

标签 swift youtube upload swift4

我从我的应用程序将视频上​​传到 youtube。但现在是公开的。

我的问题是如何上传不公开的视频?

这是我的上传功能

func postVideoToYT(videoUrl: URL, token: String,title:String,innoId:Int,videoTags:String,callback: @escaping (Bool) -> Void) {
        do {
            let headers = ["Authorization": "Bearer \(token)"]
            let videoData = try Data(contentsOf: videoUrl)
            upload(multipartFormData: { multipartFormData in
                multipartFormData.append("{'snippet':{'title' : '\(title)', 'description': ''}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "snippet", mimeType: "application/json")
                multipartFormData.append(videoData, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
            }, usingThreshold: 1, to: URL(string: "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet")!, method: .post, headers: headers, encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print("Post video to url --->\(response)")
                        if let json = response.result.value as? [String : Any] {
                            let videoId = json["id"] as! String
                            self.delayWithSeconds(1, completion: {
                                self.addVideo(innoId: innoId, videoKey:videoId, shortDesc: title, tagIds: videoTags)
                            })
                        }

                        callback(true)
                    }
                    break
                case .failure(_):
                    callback(false)
                    break
                }
            })
        }

        catch {

        }
    }

最佳答案

隐私必须与代码段一起传递

"{'snippet':{'title' : '\(title)', 'description':' \(description)', 'tags': \(keywords)},'status':{'privacyStatus' : '\(privacyStatus)'}}".data(using: .utf8, allowLossyConversion: false)!

在下面的代码中添加了隐私。

func postVideoToYT(videoUrl: URL, token: String,title:String,innoId:Int,videoTags:String,callback: @escaping (Bool) -> Void) {
    do {
        let headers = ["Authorization": "Bearer \(token)"]
        let videoData = try Data(contentsOf: videoUrl)
        upload(multipartFormData: { multipartFormData in
            multipartFormData.append("{'snippet':{'title' : '\(title)', 'description':' \(description)', 'tags': \(keywords)},'status':{'privacyStatus' : '\(privacyStatus)'}}".data(using: .utf8, allowLossyConversion: false)!, withName: "snippet", mimeType: "application/json")
            multipartFormData.append(videoData, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
        }, usingThreshold: 1, to: URL(string: "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet")!, method: .post, headers: headers, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("Post video to url --->\(response)")
                    if let json = response.result.value as? [String : Any] {
                        let videoId = json["id"] as! String
                        self.delayWithSeconds(1, completion: {
                            self.addVideo(innoId: innoId, videoKey:videoId, shortDesc: title, tagIds: videoTags)
                        })
                    }

                    callback(true)
                }
                break
            case .failure(_):
                callback(false)
                break
            }
        })
    }

    catch {

    }
}

关于Swift - 如何上传视频到 youtube 不公开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781588/

相关文章:

java - Vert.X Web 客户端进度状态

python - 在Python中上传和读取csv文件

ios - 如何在图像中添加快速 Spring 动画?

ios - 如何在 swift 中为包含在 stackview 中的标签提供动态高度

youtube - 在客户端应用程序中保护 YouTube v3 API key

php - 如何使用php提取src值并解析ID号

javascript - 可以从文件上传输入获取文件 data/val 并使用 Ajax 请求将其发送到服务器

IOS swift 4展开和折叠tableview不工作并导致崩溃

ios - 在 stackview 中以编程方式定位 View 、标签、 ImageView 的问题

javascript - 显示 :none not working on iPhone simulator