使用Swift通过REST API设置YouTube上传的代码段数据 [英] Setting snippet data for youtube upload via REST API using Swift
问题描述
我可以使用以下代码通过其REST API将视频成功上传到youtube:
I'm able to successfully upload a video to youtube via their REST API using the following code:
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)
}
})
}
我现在想编辑上面的代码,以便可以设置一些<$ c最初上传的$ c>摘要数据,特别是标题
和说明
。
I now would like to edit the above code so that I can set some snippet
data on the initial upload, specifically a title
and description
.
我尝试使用另一种 Alamofire
方法与上面的方法非常相似,只是它还使用了 NSMutableURLRequest
作为参数。我制作了代码片段字典并将其设置为可变请求的 HTTPBody
属性。视频上传仍然有效,但是我的代码片段的标题和描述值仍未设置。
I tried using an alternative Alamofire
method that's very similar to the one above, only it also takes an NSMutableURLRequest
as a parameter. I crafted my snippet dictionary and set it as the HTTPBody
property of the mutable request. The video upload still works, but my snippet's title and description values are still not being set.
将视频上传到youtube时如何设置一些代码片段信息?
How can I set some snippet info when uploading the video to youtube?
推荐答案
在multipartFormData块中添加一行参数值,如下所示(将代码放在视频项之前):
Add a line to the multipartFormData block for the parameter values as follows (place the code before the video item):
multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT', 'description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"snippet", mimeType: "application/json")
发布网址也应更改为part = snippet
the post url should also be changed to part=snippet
https://www.googleapis.com/upload/youtube/v3/videos?part=snippet
ie
.POST,
"https://www.googleapis.com/upload/youtube/v3/videos?part=snippet",
headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT', 'description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"snippet", mimeType: "application/json")
multipartFormData.appendBodyPart(data: videodata, name: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
},
这篇关于使用Swift通过REST API设置YouTube上传的代码段数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!