RESTful机制将视频(和属性)上传到vimeo [英] RESTful mechanism to upload video (and properties) to vimeo

查看:107
本文介绍了RESTful机制将视频(和属性)上传到vimeo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将视频上传到Vimeo的过程与为YouTube定义的过程非常相似,但仅限于一点。下面,我描述有效的步骤,并概述不能的最后一个视频上传步骤:

The procedure to upload a video to Vimeo starts out very similarly as the one defined for Youtube, but only up to a point. Below I describe the steps that worked, and outline the last video-upload step which does not:

当我们传递以下参数以触发用户身份验证时,Vimeo上传舞蹈开始:

The Vimeo-upload dance begins when we pass the following parameters to trigger user authentication:

let authPath:String = "\(url_vauth)?response_type=\(response_type)&client_id=\(client_id)&redirect_uri=\(redirect_uri)&state=\(state)&scope=upload"

if let authURL:NSURL = NSURL(string: authPath) {
  let request = NSURLRequest(URL: authURL)
  webView.loadRequest(request)  // opens a webpage in a webUIView

  // once credentials are entered, google redirects back with the above uri + a temporary code
  // we will exchange later for a token

  // within AppDelegate, we have defined a way to handle this uri, which is to call
  // processOAuthStep1Response(url)

然后,我们处理返回的响应以提取授权代码:

Then, we process the returned response to extract an authorization code:

let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false)
var auth_code:String!
// the block below extracts the text that follows "code" in the return url

if let queryItems = components?.queryItems {
  for queryItem in queryItems { // step through each part of url
    if queryItem.name.lowercaseString == "code" {
      auth_code = queryItem.value
      break
    } //  end of if queryItem.name.lowercaseString
  } // end of for
} // if let queryItems

授权代码,然后我们生成一个令牌

With this authorization code, we then generate a token:

  let getTokenPath:String = url_token
  let grant_type = "authorization_code"
  let header_plain = "\(client_id):\(client_secret)"
  let string_plain = header_plain.dataUsingEncoding(NSUTF8StringEncoding)
  let string_base64 = (string_plain?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)))! as String
  let headers = ["Authorization": "basic \(string_base64)"]  // note that string_base64 really needs to be in base64!
  //print ("...header is: \(string_base64)")
  let tokenParams = ["grant_type": grant_type, "code": receivedCode, "redirect_uri": redirect_uri, "scope": "public"]
  let request = Alamofire.request(.POST, getTokenPath, parameters: tokenParams, encoding: .URL, headers: headers)

我们使用此令牌生成门票

request(.POST, url_getticket, parameters: ticketParams , encoding: .URL, headers: headers).responseJSON { response  in
  //print(response)
  switch response.result {
  case .Success(let data):
    let json = JSON(data)
    print (json)
    let myticket = json["ticket_id"].stringValue
    //let usage = json[("upload_quota")].stringValue
    let htmlform = json[("form")].stringValue
    let uploadlink = json[("upload_link_secure")].stringValue
    print("......ticket is \(myticket)")
    print("......form is \(htmlform)")
    print("......upload link is \(uploadlink)")
  case .Failure(let error):
    print("Request failed with error: \(error)")
  } // end of switch

最后(这是停止尖叫的地方)暂停),我们应该使用该票证向Vimeo发出 POST 请求。问题在于,该票证以html形式嵌入,实际上会向Vimeo发出上传请求...对于我尝试实现此功能的iOS平台不是很有用。理想情况下,我想通过这样的上载调用使用Alamofire来实现:

Finally (and this is where things stop to a screeching halt) we are supposed to use this ticket to make a POST request to Vimeo. The problem is that this ticket is embedded in an html form that actually makes the upload request to Vimeo...Not very useful for the iOS platform where I'm trying to implement this. Ideally, I'd like implementing with Alamofire via an upload call like so:

  let headers = ["Authorization": "Bearer \(token)"]
  upload(
      .POST,
      "https://1511923767.cloud.vimeo.com/upload?ticket_id=#######&video_file_id=####&signature=####&v6=1&redirect_url=https%3A%2F%2Fvimeo.com%2Fupload%2Fapi%3Fvideo_file_id%3D498216063%26app_id%3D70020%26ticket_id%####%26signature%######", 
      headers: headers,
      multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: videodata, name: "video", fileName: "bagsy.m4v", mimeType: "application/octet-stream")
      },
      encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
          upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
            dispatch_async(dispatch_get_main_queue()) {
              let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
              //progress(percent: percent)
              print ("................\(percent)")
            }
          }
          upload.validate()
          upload.responseJSON { response in
            print(response)
            callback(true)
          }
        case .Failure(_):
          callback(false)
        }

    })

不用说,上面的代码块不起作用。

Needless to say, the chunk of code above does not work. Any guidance would be most appreciated.

推荐答案

请考虑使用官方 Vimeo iOS上传SDK 。我们大约在2周前将其公开。这是一个Swift库,用于处理将视频文件上传到Vimeo服务器的过程。这样做是通过后台配置的NSURLSession进行的(因此,无论您的应用程序位于前台还是后台,上传都会继续进行)。如果你有问题就告诉我们。注意:我是图书馆的作者之一,我在Vimeo工作。

Consider using the official Vimeo iOS Upload SDK. We made it public about 2 weeks ago. It's a Swift library that handles upload of video files to Vimeo servers. It does so using a background-configured NSURLSession (so uploads continue regardless of whether your app is in the foreground or background). Let us know if you have any questions. Note: I'm one of the authors of the library and I work at Vimeo.

VimeoUpload 自述文件非常强大,应该传达您需要知道的所有信息。让莴苣知道您是否还有其他问题,或者随时提出请求。

The VimeoUpload README is pretty robust and should communicate all that you need to know. Lettuce know if you have additional questions though, or feel free to make a pull request.

这篇关于RESTful机制将视频(和属性)上传到vimeo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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