我可以使用以下代码通过REST API将视频成功上传到youtube:
func postVideoToYouTube(token: String,callback: Bool -> Void){ let headers = ["Authorization": "Bearer \(token)"] let path = NSBundle.mainBundle().pathForResource("video",ofType: "mp4") let videodata: NSData = NSData.dataWithContentsOfMappedFile(path!)! as! NSData upload( .POST,"https://www.googleapis.com/upload/youtube/v3/videos?part=id",headers: headers,multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(data: videodata,name: "video",fileName: "video.mp4",mimeType: "application/octet-stream") },encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload,_,_): upload.responseJSON { request,response,error in print(response) callback(true) } case .Failure(_): callback(false) } }) }
我现在想编辑上面的代码,以便我可以在初始上传时设置一些片段数据,特别是标题和描述.
我尝试使用替代Alamofire方法,该方法与上面的方法非常相似,只是它还将NSMutableuRLRequest作为参数.我精心设计了我的片段字典并将其设置为可变请求的HTTPBody属性.视频上传仍然有效,但我的片段标题和说明值仍未设置.
在将视频上传到youtube时,如何设置一些代码段信息?
在multipartFormData块中为参数值添加一行,如下所示(将代码放在视频项之前):
multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT','description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false)!,name :"snippet",mimeType: "application/json")
邮政网址也应更改为part = snippet
https://www.googleapis.com/upload/youtube/v3/videos?part=snippet
即
.POST,"https://www.googleapis.com/upload/youtube/v3/videos?part=snippet",multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT',mimeType: "application/json") multipartFormData.appendBodyPart(data: videodata,mimeType: "application/octet-stream") },