首页 > 解决方案 > YouTube Data Api Swift:通过隐私状态和标签将视频分享到 YouTube

问题描述

我的应用程序中有一个分享到 youtube 的功能。我也允许用户选择隐私状态。我可以上传带有标题和说明的视频。我无法设置视频的隐私状态和主题标签。我尝试了隐私状态,但它不起作用。我不确定如何为视频添加标签。

这是我将视频上传到 YouTube 的功能:

// MARK: Share to Platforms
func postVideoToYouTube(token: String, callback: @escaping (Bool) -> Void) {
    let headers = ["Authorization": "Bearer \(token)"]
        do {
            videoData = try Data.init(contentsOf: videoForThirdParty!)
        } catch {
            print("cannot get contents of video")
    }

    var captionText: String?

    if let captionTextStuff = captionTextField.text {
        captionText = captionTextStuff
    }

    upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append("{'snippet':{'title' : '\(captionText!)' , 'description': 'This video was made with Vloggle'}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"snippet", mimeType: "application/json")

            // This is where I set the privacy status:

            multipartFormData.append("{'status':{'privacyStatus' : 'private'}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"status", mimeType: "application/json")

            multipartFormData.append(self.videoData!, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
    }, usingThreshold: UInt64.init(),
       to: "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet",
       headers: headers,
       encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON(completionHandler: { (response) in
                print(response)
                callback(true)
            })
        case .failure(_):
            callback(false)
        }
    })
}

标签: iosswiftxcodeyoutube-apiyoutube-data-api

解决方案


推荐阅读