更改视频标题时出现 Youtube 数据 api 错误 [英] Youtube data api error when changing video title

查看:35
本文介绍了更改视频标题时出现 Youtube 数据 api 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在下面给出的脚本中出现此错误:

why did i get this error on the script given below:

Traceback (most recent call last):
  File "C:\Path\YoutubeApi\main.py", line 54, in <module>
    main()
  File "C:\Path\YoutubeApi\main.py", line 49, in main
    response = request.execute()
  File "C:\Users\$user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\googleapiclient\_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\$user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\googleapiclient\http.py", line 937, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=snippet&alt=json returned "The <code>snippet.categoryId</code> property specifies an invalid category ID. Use the <code><a href="/youtube/v3/docs/videoCategories/list">videoCategories.list</a></code> method to retrieve supported categories.". Details: "[{'message': 'The <code>snippet.categoryId</code> property specifies an invalid category ID. Use the <code><a href="/youtube/v3/docs/videoCategories/list">videoCategories.list</a></code> method to retrieve supported categories.', 'domain': 'youtube.video', 'reason': 'invalidCategoryId', 'location': 'body.snippet.categoryId', 'locationType': 'other'}]">

主.py

import os
import pickle
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build

def main():
    credentials = None
    # token.pickle stores the user's credentials from previously successful logins
    if os.path.exists('token.pickle'):
        print('Loading Credentials From File...')
        with open('token.pickle', 'rb') as token:
            credentials = pickle.load(token)

    if not credentials or not credentials.valid:
        if credentials and credentials.expired and credentials.refresh_token:
            print('Refreshing Access Token...')
            credentials.refresh(Request())
        else:
            print('Fetching New Tokens...')
            flow = InstalledAppFlow.from_client_secrets_file(
                'client_secrets.json',
                scopes=[
                    'https://www.googleapis.com/auth/youtube.force-ssl'
                ]
            )

            flow.run_local_server(port=8080, prompt='consent',
                                authorization_prompt_message='')
            credentials = flow.credentials

            # Save the credentials for the next run
            with open('token.pickle', 'wb') as f:
                print('Saving Credentials for Future Use...')
                pickle.dump(credentials, f)

    youtube = build('youtube','v3',credentials=credentials)

    request = youtube.videos().update(
        part = 'snippet',
        body={
            "id": "OSxK-tscmVA",
            "snippet":{
                "title":"It's changed",
            }
        }
    )
    

    response = request.execute()

    print(response)

if __name__ == '__main__':
    main()

脚本说明:

脚本的作用是,如果没有名为 token.pickle 的文件,它将要求用户授权应用程序,并且脚本会将用户凭据存储在 token.pickle 文件中,以便用户不必授权每次运行都会更改应用程序,脚本的第 2 部分会更改我的 YouTube 视频的标题.

What the script does is that if there's no file named token.pickle it will ask the user to authorize the application and the script will store the user credentials in token.pickle file so that the user doesn't have to authorize the application on every single run and the part 2 of the script changes my YouTube video's title.

推荐答案

不幸的是,您似乎不倾向于使用 Videos.update API 端点:

Unfortunately, you seem not inclined to make use of the official specification of the Videos.update API endpoint:

您致电的请求正文端点也需要指定视频类别 ID 才能被 API 后端接受.

The request body of your call to the endpoint needs to specify the video category ID too for to be accepted by the API back-end.

请注意update_video.py 来自 Google 的示例代码——我在你的系列帖子的开头指出你是有价值的信息来源——包含所有这些必需的东西.

Note that the update_video.py sample code from Google -- that I indicated you at the beginning of your series of posts as valuable source of information -- has all these required things in it.

再次重申,您必须吸收(即理解)该代码.除了仔细阅读官方文档之外别无他法.

Again, please acknowledge that you have to absorb (i.e. understand) that code. There's no other way than going through reading carefully the official documentation.

这篇关于更改视频标题时出现 Youtube 数据 api 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆