YouTube数据Api Swift:使用隐私状态和标签将视频分享到YouTube

时间:2018-07-11 17:54:29

标签: ios swift xcode youtube-api youtube-data-api

我在我的应用中有一个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)
        }
    })
}

0 个答案:

没有答案