How to upload video to youtube using google api. Without libraries

China☆狼群 提交于 2021-02-11 13:22:43

问题


I don't want to use libraries for this, I just want to send REST request to the API. Here is API reference for uploading videos. I don't see anywhere in the documentation where to put a video file. Should it be in the header or request body? Does anyone know how this HTTP request should look like?


回答1:


Here is the official documentation of the Resumable Upload Protocol, which is used by all of Google's public (open source) libraries.

I personally would not recommend you to implement resumable video uploading using bare HTTP request methods. That's quite tricky to done it right. But, if you don't want to use Google's libraries, you have to absorb this doc and implement its specifications the way you need it.

There's also the possibility to upload videos on one go (thus not using the above mentioned protocol). That would entail calling a POST method on the URL:

https://www.googleapis.com/upload/youtube/v3/videos?uploadType=multipart&part=snippet,status,

where you'll have to compose a multipart/related content-type as exemplified below:

POST /upload/youtube/v3/videos?uploadType=multipart&part=snippet,status HTTP/1.1
Host: www.googleapis.com
Content-length: 453
Content-type: multipart/related; boundary="===============8268018375852491166=="
Authorization: Bearer [YOUR_VALID_ACCESS_TOKEN]

--===============8268018375852491166==
Content-Type: application/json
MIME-Version: 1.0

{
  "snippet": {
    "title": "test video",
    "description": "just some test video",
    "categoryId": "22"
  },
  "status": {
    "privacyStatus": "private",
    "embeddable": false,
    "license": "youtube"
  }
}

--===============8268018375852491166==
Content-Type: video/mp4
MIME-Version: 1.0
Content-Transfer-Encoding: binary

TEST

--===============8268018375852491166==--

You could use as spring of inspiration for your own code this Python3 script I implemented a while ago. The script takes as input an JSON object specifying the metadata of the video to be uploaded and the video file itself and generates the multipart/related file and the associated Content-Type HTTP header that could well be used by an immendiate curl command. (Issue my script with --help for brief helping info.)

Note that my script is based on Google's own open source code, specifically on discovery.py. This latter script is part of Google's APIs Client Library for Python.



来源:https://stackoverflow.com/questions/65258438/how-to-upload-video-to-youtube-using-google-api-without-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!