Youtube 视频插入 returns "default" 视频资源
Youtube Video Insert returns "default" video resource
我正在尝试将视频从 S3 存储桶上传到 YouTube,并返回暗示成功的奇怪输出 post,但没有返回任何预期的结果。同样,我在我的代码中设置了 title
和 description
等属性,但正如您从输出中看到的那样,这实际上并没有被设置。
示例输出:
{
"id": "-pfZ_BNH9kg",
"snippet": {
"channelId": "UCZ5AUe-rp3rXKeFS0yx4ZBA",
"title": "unknown",
"channelTitle": "Patrick Hanford",
"publishedAt": "2020-04-30T19:22:15.000Z",
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/hqdefault.jpg",
"height": 360,
"width": 480
},
"default": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/default.jpg",
"height": 90,
"width": 120
},
"medium": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/mqdefault.jpg",
"height": 180,
"width": 320
}
},
"localized": {
"title": "unknown",
"description": ""
},
"liveBroadcastContent": "none",
"categoryId": "20",
"description": ""
},
"etag": "Dn5xIderbhAnUk5TAW0qkFFir0M/3T1YGvGo1YyaTKtTpl8JrJqWS4M",
"status": {
"embeddable": true,
"privacyStatus": "public",
"uploadStatus": "uploaded",
"publicStatsViewable": true,
"license": "youtube"
},
"kind": "youtube#video"
}
上传代码:
def post(self, attempts=None):
TEST_VIDEO = "http://streamon-perm.s3.amazonaws.com/WPHM-48k-pl-33366.mp4"
headers = {"Content-Type": "video/mp4"}
upload_request_body = {
"snippet": {
"title": "Test Video Upload",
"description": "This is a test of uploading videos.",
"categoryId": "22",
},
"status": {
"privacyStatus": "public"
},
"fileDetails": {
"fileName": TEST_VIDEO,
"fileType": "video"
}
}
params = {
"access_token": self.google_token.get("access_token", None),
"id": self.google_token.get("id_token", None),
"part": "snippet, status"
}
extra = {
"client_id": self.client_id,
"client_secret": self.client_secret
}
google_oauth_session = OAuth2Session(
self.client_id,
token=self.google_token,
auto_refresh_url=self.token_url,
auto_refresh_kwargs=extra,
token_updater=self._save_token
)
upload_response = google_oauth_session.post(
self.video_post_url,
headers=headers,
json=upload_request_body,
params=params
)
logger.info("Response from VIDEO UPLOAD: %s", repr(upload_response.content))
return True
我也试过从S3下载文件直接上传,结果一样。如果没有正确的错误消息或任何可以解决的问题,我真的不确定接下来要尝试什么。非常感谢任何帮助。
我也试过单独使用 requests
而不是使用 oauthlib
得到完全相同的结果。
def post(self, attempts=None):
if attempts is None:
attempts = 0
if self.neutered:
msg = "Youtube post() disabled by ENVIRONMENT variables."
logger.info(msg)
return msg
logger.info("Youtube post() entered with attempt # %s", self.post_attempts)
if self.google_token is None:
self.google_token = self._set_google_token()
attempts += 1
self.post(attempts=attempts)
headers = {
"Content-Type": "video/mp4",
"client_id": self.client_id,
"client_secret": self.client_secret,
"Authorization": "Bearer " + self.google_token["access_token"]
}
params = {
"access_token": self.google_token.get("access_token", None),
"id": self.google_token.get("id_token", None),
"part": "snippet, status"
}
upload_request_body = {
"snippet": {
"title": "Test Video Upload",
"description": "This is a test of uploading videos from POST.",
"categoryId": "22",
},
"status": {
"privacyStatus": "public"
},
"fileDetails": {
"fileName": TEST_VIDEO,
"fileType": "video"
}
}
upload_response = requests.post(
self.video_post_url,
params=params,
headers=headers,
json=upload_request_body
)
logger.info("Response from VIDEO UPLOAD: %s", repr(upload_response.content))
return True
I have also tried downloading the file from S3 and uploading with the file directly, and I get the same result.
您遇到此问题可能是因为您实际上并未发送文件。 upload_request_body.fileDetails.fileName
不是 link/file 的位置。这只是一个描述属性。
您是否尝试过 https://developers.google.com/youtube/v3/code_samples/code_snippets 自动生成的验证码?
这是您可以到达那里的内容:
# -*- coding: utf-8 -*-
# Sample Python code for youtube.videos.insert
# NOTES:
# 1. This sample code uploads a file and can't be executed via this interface.
# To test this code, you must run it locally using your own API credentials.
# See: https://developers.google.com/explorer-help/guides/code_samples#python
# 2. This example makes a simple upload request. We recommend that you consider
# using resumable uploads instead, particularly if you are transferring large
# files or there's a high likelihood of a network interruption or other
# transmission failure. To learn more about resumable uploads, see:
# https://developers.google.com/api-client-library/python/guide/media_upload
import os
import googleapiclient.discovery
from googleapiclient.http import MediaFileUpload
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = "YOUR_API_KEY"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
request = youtube.videos().insert(
part="snippet,status",
body={
"fileDetails": {
"fileName": "qwer",
"fileType": "video"
},
"snippet": {
"categoryId": "22",
"description": "This is a test of uploading videos.",
"title": "Test Video Upload"
},
"status": {
"privacyStatus": "public"
}
},
# TODO: For this request to work, you must replace "YOUR_FILE"
# with a pointer to the actual file you are uploading.
media_body=MediaFileUpload("YOUR_FILE")
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
我相信它应该有效。
或者有什么理由不使用googleapiclient
?
I'm trying to upload a video from an S3 bucket to YouTube
我怀疑您是否可以将文件从其他站点直接上传到 Youtube。可能您无法选择从您自己的 server/drive 上传文件。我在 Internet 上查找过,但我发现的是你不能(尽管你过去可以)。可以想象为什么不允许这样做的原因有很多(主要是版权,但不是排他性的)。
更新:
这可能不是详尽无遗的代码片段。特别是考虑到您需要 OAuth2.
但这是另一个:
https://github.com/youtube/api-samples/blob/master/python/upload_video.py
还有一个:
https://developers.google.com/youtube/v3/guides/uploading_a_video
使用 OAuth2。您还可以在那里找到有关 client_secrets.json
.
的信息
{
"web": {
"client_id": "[[INSERT CLIENT ID HERE]]",
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
"redirect_uris": [],
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token"
}
}
您还可以查看一些现实生活中的项目。例如这个:https://github.com/HA6Bots/Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader/tree/master/Youtube%20Bot%20Video%20Generator
我正在尝试将视频从 S3 存储桶上传到 YouTube,并返回暗示成功的奇怪输出 post,但没有返回任何预期的结果。同样,我在我的代码中设置了 title
和 description
等属性,但正如您从输出中看到的那样,这实际上并没有被设置。
示例输出:
{
"id": "-pfZ_BNH9kg",
"snippet": {
"channelId": "UCZ5AUe-rp3rXKeFS0yx4ZBA",
"title": "unknown",
"channelTitle": "Patrick Hanford",
"publishedAt": "2020-04-30T19:22:15.000Z",
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/hqdefault.jpg",
"height": 360,
"width": 480
},
"default": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/default.jpg",
"height": 90,
"width": 120
},
"medium": {
"url": "https://i.ytimg.com/vi/-pfZ_BNH9kg/mqdefault.jpg",
"height": 180,
"width": 320
}
},
"localized": {
"title": "unknown",
"description": ""
},
"liveBroadcastContent": "none",
"categoryId": "20",
"description": ""
},
"etag": "Dn5xIderbhAnUk5TAW0qkFFir0M/3T1YGvGo1YyaTKtTpl8JrJqWS4M",
"status": {
"embeddable": true,
"privacyStatus": "public",
"uploadStatus": "uploaded",
"publicStatsViewable": true,
"license": "youtube"
},
"kind": "youtube#video"
}
上传代码:
def post(self, attempts=None):
TEST_VIDEO = "http://streamon-perm.s3.amazonaws.com/WPHM-48k-pl-33366.mp4"
headers = {"Content-Type": "video/mp4"}
upload_request_body = {
"snippet": {
"title": "Test Video Upload",
"description": "This is a test of uploading videos.",
"categoryId": "22",
},
"status": {
"privacyStatus": "public"
},
"fileDetails": {
"fileName": TEST_VIDEO,
"fileType": "video"
}
}
params = {
"access_token": self.google_token.get("access_token", None),
"id": self.google_token.get("id_token", None),
"part": "snippet, status"
}
extra = {
"client_id": self.client_id,
"client_secret": self.client_secret
}
google_oauth_session = OAuth2Session(
self.client_id,
token=self.google_token,
auto_refresh_url=self.token_url,
auto_refresh_kwargs=extra,
token_updater=self._save_token
)
upload_response = google_oauth_session.post(
self.video_post_url,
headers=headers,
json=upload_request_body,
params=params
)
logger.info("Response from VIDEO UPLOAD: %s", repr(upload_response.content))
return True
我也试过从S3下载文件直接上传,结果一样。如果没有正确的错误消息或任何可以解决的问题,我真的不确定接下来要尝试什么。非常感谢任何帮助。
我也试过单独使用 requests
而不是使用 oauthlib
得到完全相同的结果。
def post(self, attempts=None):
if attempts is None:
attempts = 0
if self.neutered:
msg = "Youtube post() disabled by ENVIRONMENT variables."
logger.info(msg)
return msg
logger.info("Youtube post() entered with attempt # %s", self.post_attempts)
if self.google_token is None:
self.google_token = self._set_google_token()
attempts += 1
self.post(attempts=attempts)
headers = {
"Content-Type": "video/mp4",
"client_id": self.client_id,
"client_secret": self.client_secret,
"Authorization": "Bearer " + self.google_token["access_token"]
}
params = {
"access_token": self.google_token.get("access_token", None),
"id": self.google_token.get("id_token", None),
"part": "snippet, status"
}
upload_request_body = {
"snippet": {
"title": "Test Video Upload",
"description": "This is a test of uploading videos from POST.",
"categoryId": "22",
},
"status": {
"privacyStatus": "public"
},
"fileDetails": {
"fileName": TEST_VIDEO,
"fileType": "video"
}
}
upload_response = requests.post(
self.video_post_url,
params=params,
headers=headers,
json=upload_request_body
)
logger.info("Response from VIDEO UPLOAD: %s", repr(upload_response.content))
return True
I have also tried downloading the file from S3 and uploading with the file directly, and I get the same result.
您遇到此问题可能是因为您实际上并未发送文件。 upload_request_body.fileDetails.fileName
不是 link/file 的位置。这只是一个描述属性。
您是否尝试过 https://developers.google.com/youtube/v3/code_samples/code_snippets 自动生成的验证码? 这是您可以到达那里的内容:
# -*- coding: utf-8 -*-
# Sample Python code for youtube.videos.insert
# NOTES:
# 1. This sample code uploads a file and can't be executed via this interface.
# To test this code, you must run it locally using your own API credentials.
# See: https://developers.google.com/explorer-help/guides/code_samples#python
# 2. This example makes a simple upload request. We recommend that you consider
# using resumable uploads instead, particularly if you are transferring large
# files or there's a high likelihood of a network interruption or other
# transmission failure. To learn more about resumable uploads, see:
# https://developers.google.com/api-client-library/python/guide/media_upload
import os
import googleapiclient.discovery
from googleapiclient.http import MediaFileUpload
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = "YOUR_API_KEY"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
request = youtube.videos().insert(
part="snippet,status",
body={
"fileDetails": {
"fileName": "qwer",
"fileType": "video"
},
"snippet": {
"categoryId": "22",
"description": "This is a test of uploading videos.",
"title": "Test Video Upload"
},
"status": {
"privacyStatus": "public"
}
},
# TODO: For this request to work, you must replace "YOUR_FILE"
# with a pointer to the actual file you are uploading.
media_body=MediaFileUpload("YOUR_FILE")
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
我相信它应该有效。
或者有什么理由不使用googleapiclient
?
I'm trying to upload a video from an S3 bucket to YouTube
我怀疑您是否可以将文件从其他站点直接上传到 Youtube。可能您无法选择从您自己的 server/drive 上传文件。我在 Internet 上查找过,但我发现的是你不能(尽管你过去可以)。可以想象为什么不允许这样做的原因有很多(主要是版权,但不是排他性的)。
更新:
这可能不是详尽无遗的代码片段。特别是考虑到您需要 OAuth2.
但这是另一个:
https://github.com/youtube/api-samples/blob/master/python/upload_video.py
还有一个:
https://developers.google.com/youtube/v3/guides/uploading_a_video
使用 OAuth2。您还可以在那里找到有关 client_secrets.json
.
{ "web": { "client_id": "[[INSERT CLIENT ID HERE]]", "client_secret": "[[INSERT CLIENT SECRET HERE]]", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
您还可以查看一些现实生活中的项目。例如这个:https://github.com/HA6Bots/Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader/tree/master/Youtube%20Bot%20Video%20Generator