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

huangapple go评论73阅读模式
英文:

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 = &#39;API key&#39;
file =&#39;./path/to/file.mp4&#39;

def upload_file():
    youtube = build(&#39;youtube&#39;, &#39;v3&#39;, developerKey=API_KEY)
    media_body=MediaFileUpload(file, chunksize=-1, resumable=True)
    body=dict(
        snippet=dict(
            title=&quot;test_title&quot;,
            description=&quot;something random description&quot;,
            tags=[&quot;soccer&quot;, &quot;sports&quot;, &quot;funny&quot;],
            categoryId=&quot;22&quot;
        ),
        status=dict(
            privacyStatus=&quot;private&quot;
        )
    )

    youtube.videos().insert(
        part=&quot;,&quot;.join(body.keys()),
        body=body,
        media_body=media_body,
    ).execute()

upload_file()

With the provided code I receive the following error message:

&lt;HttpError 401 when requesting None returned &quot;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&quot;. Details: &quot;[{&#39;message&#39;: &#39;Login Required.&#39;, &#39;domain&#39;: &#39;global&#39;, &#39;reason&#39;: &#39;required&#39;, &#39;location&#39;: &#39;Authorization&#39;, &#39;locationType&#39;: &#39;header&#39;}]&quot;&gt;

It should be a server side script, so without any user interaction for login for etc.

答案1

得分: 1

如果您查看文档中的视频插入方法,您会发现它要求进行授权。

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

授权要求您请求用户的许可以访问其数据。在您的情况下,要插入到用户的YouTube帐户中,您需要得到他们的许可。

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

英文:

If you check the Videos insert method in the documentation. You will find that it states that it requires authorization

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

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.

huangapple
  • 本文由 发表于 2023年4月4日 18:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定