Mit*_*ell 3 youtube rest video upload swift
编辑:经常检查这个,当我或其他人帮我解决时,将标记为已解决!
我正在尝试通过Swift将YouTube视频上传到YouTube的REST API,但我很难搞清楚要做什么.我目前有一个有效的GET请求.
我对如何构造POST请求URL以及文件位置在请求中的位置感到困惑.另外我想我应该使用可重新上传的协议?
我已经在各种API和文档中苦苦挣扎了2天,感到绝望.
这是我的GET请求的工作代码.
func getRequestVideoInfo(){
// Set up your URL
let youtubeApi = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=AKiiekaEHhI&key=" + apiKey
let url = NSURL(string: youtubeApi)
// Create your request
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? [String : AnyObject] {
print("Response from YouTube: \(jsonResult)")
}
}
catch {
print("json error: \(error)")
}
})
// Start the request
task.resume()
}
Run Code Online (Sandbox Code Playgroud)
小智 8
是的,您需要在YouTube中创建应用/项目,并使用OAuth 2.0 Flow将视频发布/插入您获得授权访问权限的频道.
一旦你从GOOGLE获得了访问权限
使用Alamofire如下:
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)
}
})
}
Run Code Online (Sandbox Code Playgroud)
像这样调用post函数:
postVideoToYouTube(accessToken, callback: { success in
if success { }
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4429 次 |
| 最近记录: |