英文:
Python Youtube API v3 Uploading Video with API Key
问题
以下是您要的代码翻译部分:
Is it possible to upload videos to Youtube with python through the Youtube v3 API and an API Key?
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
API_KEY = 'API key'
file ='./path/to/file.mp4'
def upload_file():
youtube = build('youtube', 'v3', developerKey=API_KEY)
media_body=MediaFileUpload(file, chunksize=-1, resumable=True)
body=dict(
snippet=dict(
title="test_title",
description="something random description",
tags=["soccer", "sports", "funny"],
categoryId="22"
),
status=dict(
privacyStatus="private"
)
)
youtube.videos().insert(
part=",".join(body.keys()),
body=body,
media_body=media_body,
).execute()
upload_file()
With the provided code I receive the following error message:
<HttpError 401 when requesting None returned "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See https://cloud.google.com/docs/authentication". Details: "[{'message': 'Login Required.', 'domain': 'global', 'reason': 'required', 'location': 'Authorization', 'locationType': 'header'}]">
英文:
Is it possible to upload videos to Youtube with python through the Youtube v3 API and an API Key?
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
API_KEY = 'API key'
file ='./path/to/file.mp4'
def upload_file():
youtube = build('youtube', 'v3', developerKey=API_KEY)
media_body=MediaFileUpload(file, chunksize=-1, resumable=True)
body=dict(
snippet=dict(
title="test_title",
description="something random description",
tags=["soccer", "sports", "funny"],
categoryId="22"
),
status=dict(
privacyStatus="private"
)
)
youtube.videos().insert(
part=",".join(body.keys()),
body=body,
media_body=media_body,
).execute()
upload_file()
With the provided code I receive the following error message:
<HttpError 401 when requesting None returned "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See https://cloud.google.com/docs/authentication". Details: "[{'message': 'Login Required.', 'domain': 'global', 'reason': 'required', 'location': 'Authorization', 'locationType': 'header'}]">
It should be a server side script, so without any user interaction for login for etc.
答案1
得分: 1
如果您查看文档中的视频插入方法,您会发现它要求进行授权。
授权要求您请求用户的许可以访问其数据。在您的情况下,要插入到用户的YouTube帐户中,您需要得到他们的许可。
另一方面,API密钥仅用于获取只读访问权限以访问公共数据。您不能使用它访问私人用户帐户。
英文:
If you check the Videos insert method in the documentation. You will find that it states that it requires authorization
Authorization requires that you request permission of the user to access their data. In your case to insert into a users YouTube account you need their permission.
API keys on the other hand are only used to get read only access access public data. You cant access a private user account with it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论