Setting snippet data for youtube upload via REST API using Swift

怎甘沉沦 提交于 2019-11-29 12:58:53

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!