Python Youtube API v3 使用 API 密钥上传视频

问题描述 投票:0回答:1

是否可以通过 Youtube v3 API 和 API 密钥使用 python 将视频上传到 Youtube?

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()

使用提供的代码,我收到以下错误消息:

<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'}]">

它应该是一个服务器端脚本,因此不需要任何用户交互来登录等。

python python-3.x oauth youtube-data-api
1个回答
2
投票

如果您检查文档中的Videos insert方法。你会发现它注明需要授权

enter image description here

授权要求您请求用户允许访问其数据。如果您要插入用户的 YouTube 帐户,您需要获得他们的许可。

另一方面,API 密钥仅用于获取公共数据的只读访问权限。 您无法使用它访问私人用户帐户。

© www.soinside.com 2019 - 2024. All rights reserved.