Insert a Comment Using the Youtube API and Alamofire

Insert a comment using the YouTube API and Alamofire

As I saw in your other post (Google API - Invalid Credentials) you know how to make an authenticated Alamofire request. Now you need to build a proper parameters dictionary to meet the API requirements. I looked into the Youtube Data API guide.

This is the example of a JSON body provided in the documentation for adding a comment:

{
"snippet": {
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"topLevelComment": {
"snippet": {
"textOriginal": "This video is awesome!"
}
},
"videoId": "MILSirUni5E"
}
}

Let's build a parameters dictionary based on the above example, it is a nested dictionary:

let commentParams: Parameters = ["textOriginal": "This video is awesome!"]
let snippetParams: Parameters = ["snippet": commentParams]
let topLevelSnippet: Parameters = [
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"topLevelComment": snippetParams,
"videoId": "MILSirUni5E"]

let allParams: Parameters = ["snippet": topLevelSnippet]

Then create your headers, your request and pass the parameters to the request

let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"]
// As API requires "part" is added as url parameter
let path = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet"
let request = Alamofire.request(path, method: HTTPMethod.post, parameters: allParams, encoding: JSONEncoding.default, headers: headers)

You should check which parameters are mandatory and which ones are not, but the idea is to build a proper parameters dictionary based on their requirements.

Insert Description with Line Breaks via YouTube Data API

Be careful with force casting variables like you're doing, I would recommend you to do something like this:

if let price = price as? String, let package = package as? String {
myDescription = "\(price)\n\(package)"
}

This way you're safe unwraping and also using String Interpolation to construct the string, you can read more in the documentation.

Multiple Arguments in AlamoFire.Request

Alamofire does a great job of formatting parameters to a request url. Just as you pass parameters with the .post method to the AF request function you also pass a parameters [string : any] to the AF request for .get methods as well. The difference is .post will put the parameters in the request.body/data vs. .get will format the parameters into the url with the ?q=my_search_string

something along the lines of this:

Let params: [string :any] = ["q": "odesza", "type":"track"]
AF.request(.GET, url, parameters:params, headers: headers) .responseJSON { response in debugPrint(response

How to separate request body in alamofire?

From the Youtube documentation it requires json body, so you need to use JSON Encoding and need to add the url parameters to the request url.

let url = "https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet,status"
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
if let response = response.result.value {
print(response)
}
}

Not getting response after using Swift 3 and Alamofire 4

you have tried with encoding: JSONEncoding.default.

encoding method should be URLEncoding.default



Related Topics



Leave a reply



Submit