Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...