I'm interested in uploading a video to youtube along with title, description, and keywords. The code below uploads a video to youtube without any properties:
func postVideoToYouTube(token: String, callback: Bool -> Void){
let headers = ["Authorization": "Bearer \(token)"]
let urlYoutube = "https://www.googleapis.com/upload/youtube/v3/videos?part=id"
let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4")
let videodata: NSData = NSData.dataWithContentsOfMappedFile(path!)! as! NSData
upload(
.POST,
urlYoutube,
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've been trying to modify urlYoutube to include the necessary snippet information to no avail:
let snippetTitle = "The Best Video Ever"
let snippetDesc = "first video upload with title"
let snippetTags = "best,video,ever,snoopy,monkey,charlie"
let urlYoutube = "https://www.googleapis.com/upload/youtube/v3/videos?part=id&snippet.title=%@&snippet.description=%@&snippet.keywords=%@", snippetTitle, snippetDesc, snippetTags)"
The other approach I tried (thanks to @adjuremods suggestion) was to use Request Body based on the Youtube-API to EDIT a previously uploaded video. So, first, I defined a video resource:
let parms = [
"kind": "youtube#video",
"id" : returnedId,
"snippet.title" : "summer vacation cali",
"snippet.description" : "had fun in the sun",
"snippet.tags" : ["surf","fun","sun"],
"snippet.categoryId" : "1"
]
and sent it as a PUT request like so:
do {
let parmsJson = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
let putURL = NSURL(string: "https://www.googleapis.com/upload/youtube/v3/videos")!
let request = NSMutableURLRequest(URL: putURL)
request.HTTPMethod = "PUT"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = parmsJson
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
}
task.resume()
//return task
} catch {
print ("...could not accomplish put request")
}
Unfortunately, the result I get is always the same, regardless of how I modify the video resource:
Result -> Optional(["error": {
code = 400;
errors = (
{
domain = global;
message = "Unsupported content with type: application/json; charset=UTF-8";
reason = badContent;
}
);
message = "Unsupported content with type: application/json; charset=UTF-8";
Could someone advice where I might be going wrong? I don't have a very clear understanding of how to set these parameters defined by the API:
https://developers.google.com/youtube/v3/docs/videos/insert#parameters