Upload Video to YouTube in Swift

后端 未结 2 2009
萌比男神i
萌比男神i 2020-12-09 14:05

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

I am trying to upload a video to YouTube with YouTube’s REST

2条回答
  •  天命终不由人
    2020-12-09 14:48

    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.


    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

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

    Call the post function like this:

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

提交回复
热议问题