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)
}
})
}
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
.
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.
How can I set some snippet info when uploading the video to youtube?
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")
the post url should also be changed to part=snippet
https://www.googleapis.com/upload/youtube/v3/videos?part=snippet
i.e.
.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")
},
The only way that I was able to upload a video with snippet data was through a combination of POST and PUT calls. POST to upload a video with no metadata, and PUT to edit the video's metadata. Doing this requires a couple of things:
1. Enable the necessary scopes
-https://www.googleapis.com/auth/youtube.upload for POST
-https://www.googleapis.com/auth/youtube for PUT
2. Be mindful of the URL's you use for each request
-https://www.googleapis.com/upload/youtube/v3/videos?part=snippet for POST
-https://www.googleapis.com/youtube/v3/videos?part=snippet&key=\(ios_key) for PUT
Now, if you're using Alamofire the POST you've included in your question can remain as is. For the PUT portion, you have to be mindful of how Alamofire wants the parameters to be passed:
request(<#T##method: Method##Method#>, <#T##URLString: URLStringConvertible##URLStringConvertible#>, parameters: <#T##[String : AnyObject]?#>, encoding: <#T##ParameterEncoding#>, headers: <#T##[String : String]?#>)
Notice parameters: <[String : AnyObject]?> This is exactly how you have to define your snippet dictionary:
let dictionarySnippet :Dictionary<String, AnyObject> = [
"title" : "something in the way",
"description" : "is this finally gonna work?",
"tags" : ["whisky","tango","fox"],
"categoryId" : "1"
]
You'll also have to send the uploaded video's id (which you can capture from your POST in yet another dictionary:
let dictionaryParameters :Dictionary<String, AnyObject> = [
"id" : "\(returnedId)",
"snippet" : dictionarySnippet,
]
Finally, you are now ready to send a PUT request that will update your video's metadata:
let putRequest = request(.PUT, "https://www.googleapis.com/youtube/v3/videos?part=snippet&key=\(ios_key)", parameters: dictionaryParameters, encoding: .JSON , headers: headers)
来源:https://stackoverflow.com/questions/36190397/setting-snippet-data-for-youtube-upload-via-rest-api-using-swift