在 Swift 中将视频上传到 YouTube [英] Upload Video to YouTube in Swift

查看:15
本文介绍了在 Swift 中将视频上传到 YouTube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然经常检查这个问题,当我或其他人帮助我解决这个问题时,将标记为已解决!

Still checking this frequently, will mark as solved when I or someone else helps me figure it out!

我正在尝试通过 Swift 使用 YouTube 的 REST API 将视频上传到 YouTube,但我很难弄清楚该怎么做.我目前有一个有效的 GET 请求.

I am trying to upload a video to YouTube with YouTube’s REST API via Swift but I am having great difficulty figuring out what to do. I currently have a working GET request.

我对应该如何构建 POST 请求 URL 以及文件位置在请求中的位置感到困惑.另外我认为我应该使用可恢复上传协议?

I'm confused on how the POST request URL should be constructed and where the file location is meant to go in the request. Also I think I should be using the resumbable upload protocol?

我已经在各种 API 和文档中苦苦挣扎了 2 天,现在感到绝望了.

I have been struggling through various API's and documentation for 2 days now and am feeling hopeless.

这是我的 GET 请求的工作代码.

Here is my working code for a GET request.

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()
}

推荐答案

HEADS UP(截至 2019 年 10 月 6 日)YouTube 已将 API 配额减少到每天 10,000 个单位.这相当于在 24 小时内上传 4 个 YouTube 视频.如果您的应用程序用例依赖于上传许多 YouTube 视频,我强烈建议您重新考虑.您可以申请扩大您的每日配额,但众所周知,Google 回复您的速度很慢,如果他们愿意的话.

HEADS UP (as of October 6, 2019) YouTube has decreased the API quota to 10,000 units per day. This is the equivalent of uploading 4 YouTube videos during a 24 hour time span. If your application use case relies on uploading many YouTube videos, I strongly advise you to reconsider. You can apply for an expansion of your daily quota, but Google is notoriously slow at getting back to you, if they do at all.

是的,您需要在 YouTube 中创建一个应用/项目并使用 OAuth 2.0 流程将视频发布/插入到您获得授权访问的频道.

Yes, you need to create an app/project in YouTube and use the OAuth 2.0 Flow to post/insert videos to a channel to which you receive authorized access.

一旦你从谷歌获得访问令牌

ONCE YOU HAVE YOUR ACCESS TOKEN FROM GOOGLE

按如下方式使用 Alamofire:

use Alamofire as follows:

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)
            }
        })
}

像这样调用 post 函数:

Call the post function like this:

postVideoToYouTube(accessToken, callback: { success in
if success { }
})

这篇关于在 Swift 中将视频上传到 YouTube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆