【发布时间】:2020-12-17 17:29:39
【问题描述】:
我正在使用 YouTube API V3 在 YouTube 上上传可恢复的视频。执行代码时,响应的 status_code 为 200(但根据官方文档,如果 API 工作正常,则需要 201)。 当我打开 YouTube 频道时,那里的视频显示“0% 处理”。 此外,一段时间后,会出现一条消息“处理已放弃”(如所附屏幕截图所示)。 我试图在脚本中包含所需的参数,但没有任何方法可以改变 YouTube 频道上视频的处理级别。 这里的代码如下:
import requests
import json
import httplib2
import sys
class youtube:
#Access token obtained from OAuth 2.0 Playground
token = 'ya29.a0Af................................'
def resumable_uploads(self):
token = self.token
url = 'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails'
headers = {'Authorization': 'Bearer {}'.format(token), 'host': 'www.googleapis.com', 'content-type': 'application/json; charset=UTF-8', 'X-Upload-Content-Type': 'video/*'}
body = {
"snippet": {
"title": "Video 2 by Sidra ",
"description": "This is a description of my video",
"tags": ["cool", "video", "more keywords"],
"categoryId": 22
},
"status": {
"privacyStatus": "public",
"embeddable": True,
"license": "youtube",
"selfDeclaredMadeForKids" : True
}
}
response = requests.request('POST', url, headers=headers, data=json.dumps(body))
print("status_code: ", response.status_code)
if response.status_code == 200:
location = response.headers['location']
headers = {'Authorization': 'Bearer {}'.format(token),'content-type': 'video/*'}
#converting file into Bytes
video_file= open("testVideo.mp4", "rb")
file_content = video_file.read()
video_file.close()
body = {
"videoFile" : file_content
}
response = requests.request('PUT', location, headers=headers, data=body)
print(response.text)
print("status_code: ", response.status_code)
obj = youtube()
obj.resumable_uploads()
控制台的响应如下:
status_code: 200
{
"kind": "youtube#video",
"etag": "0rZIqeGvCifiyfQymb8CKXsC6CI",
"id": "YF2DQDACB4M",
"snippet": {
"publishedAt": "2020-08-28T17:29:12Z",
"channelId": "UCXonQaqsiqTV_YqYsB6W4Dg",
"title": "Video 2 by Sidra",
"description": "This is a description of my video",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/YF2DQDACB4M/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/YF2DQDACB4M/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/YF2DQDACB4M/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Sidra Sial",
"tags": [
"cool",
"video",
"more keywords"
],
"categoryId": "22",
"liveBroadcastContent": "none",
"localized": {
"title": "Video 2 by Sidra",
"description": "This is a description of my video"
}
},
"contentDetails": {
"duration": "P0D",
"dimension": "2d",
"definition": "sd",
"caption": "false",
"licensedContent": false,
"contentRating": {},
"projection": "rectangular",
"hasCustomThumbnail": false
},
"status": {
"uploadStatus": "uploaded",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true,
"selfDeclaredMadeForKids": true
}
}
status_code: 200
【问题讨论】: