星期四, 2月 16, 2017

[Objective-C] 自幹上傳 mutipart/form 到youtube

要測試一下上傳影片到youtube
由於先前有在java自幹過一次 mutipart/form(四年前了@@) ,
這次改用objective c,順道記錄一下。

Youtube Upload API

函式庫


沒用什麼第三方的庫,NSURLSession自幹

NSURLSessionConfiguration : 做授權

NSMutableURLRequest : 做連線

NSURLSessionUploadTask : 上傳的任務

基本上參考這篇就會寫了
NSURLSessionUploadTask(上传任务)


流程參考

這裡要注意request body跟上傳的endpoint 是比較不一樣的,多了upload XD

Updated version: Now with custom upload url and sending of metadata with the upload process. The entire process requires 2 requests:
  1. Get a custom upload location
    First, make a POST request for an upload url to:
    "https://www.googleapis.com/upload/youtube/v3/videos"
    You will need to send 2 headers:
    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
    "Content-type": "application/json"
    You need to send 3 parameters: (沒試過uploadType)
    "uploadType": "resumable"
    "part": "snippet, status"
    "key": {YOUR_API_KEY}
    And you will need to send the metadata for the video in the request body:
        {
            "snippet": {
                "title": {VIDEO TITLE},
                "description": {VIDEO DESCRIPTION},
                "tags": [{TAGS LIST}],
                "categoryId": {YOUTUBE CATEGORY ID}
            },
            "status": {
                "privacyStatus": {"public", "unlisted" OR "private"}
            }
        }

設定影片的snippet.title踩雷

最後做一個如下圖的模擬才正確設定影片的title
一樣模擬一樣text field的欄任,然後資料就送json string。試了很多種終於成功~XD

回傳結果

重覆的影片內容還會被偵測到XD


{
 "kind": "youtube#video",
 "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/UXTZ1WX2E5Fa0kYSvpyTeyKT9LQ\"",
 "id": "你的影片id",
 "snippet": {
  "publishedAt": "2017-02-16T08:54:52.000Z",
  "channelId": "UC3b43KuPKlmMa0-s7mlPh9g",
  "title": "youtube upload video @ 2017-02-16T16:54:51+08:00",
  "description": "youtube upload video @ 2017-02-16T16:54:51+08:00",
  "thumbnails": {
   "default": {
    "url": "https://i.ytimg.com/vi/bddGhj46KqY/default.jpg",
    "width": 120,
    "height": 90
   },
   "medium": {
    "url": "https://i.ytimg.com/vi/bddGhj46KqY/mqdefault.jpg",
    "width": 320,
    "height": 180
   },
   "high": {
    "url": "https://i.ytimg.com/vi/bddGhj46KqY/hqdefault.jpg",
    "width": 480,
    "height": 360
   }
  },
  "channelTitle": "蔡大痣",
  "categoryId": "22",
  "liveBroadcastContent": "none",
  "localized": {
   "title": "youtube upload video @ 2017-02-16T16:54:51+08:00",
   "description": "youtube upload video @ 2017-02-16T16:54:51+08:00"
  }
 },
 "status": {
  "uploadStatus": "uploaded",
  "privacyStatus": "public",
  "license": "youtube",
  "embeddable": true,
  "publicStatsViewable": true
 }

}

參考

http://www.jianshu.com/p/c50e9fcd6600
http://hayageek.com/ios-nsurlsession-example/
http://blog.csdn.net/hello_hwc/article/details/44833683
http://pimacun.github.io/foundation/life-cycle-of-a-url-session
http://ecowork-blog.logdown.com/posts/160123-ios-7-nsurlsession

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails