首页 > 解决方案 > 为什么 YouTube 数据 API 中存在缺失部分错误

问题描述

为什么 YouTube 数据 API 脚本中会出现此错误:

Traceback (most recent call last):
  File "C:\Path\YoutubeApi\main.py", line 54, in <module>
    main()
  File "C:\Path\YoutubeApi\main.py", line 40, in main
    request = youtube.videos().update(
  File "C:\Users\$user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\googleapiclient\discovery.py", line 1012, in method
    raise TypeError('Missing required parameter "%s"' % name)
TypeError: Missing required parameter "part"

控制台中文件的总输出:

Loading Credentials From File...
Refreshing Access Token...
Traceback (most recent call last):
  File "C:\Path\YoutubeApi\main.py", line 54, in <module>
    main()
  File "C:\Path\YoutubeApi\main.py", line 40, in main
    request = youtube.videos().update(
  File "C:\Users\$user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\googleapiclient\discovery.py", line 1012, in method
    raise TypeError('Missing required parameter "%s"' % name)
TypeError: Missing required parameter "part"

主文件

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():
    # part 1 : store the user credentials
    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'
                ]
            )
            #  part 2 : change the video title and run the server
            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(
        body={
            "id": "OSxK-tscmVA",
            "snippet":{
                "title":"It's Changed!",
            }
        }
    )

    response = request.execute()

    print(response)

if __name__ == '__main__':
    main()

脚本说明:

脚本的作用是,如果没有命名的文件token.pickle,它将要求用户授权应用程序,并且脚本会将用户凭据存储在token.pickle文件中,这样用户就不必在每次运行时都授权应用程序, part 2并且脚本更改了我的 YouTube 视频的标题。

标签: pythonyoutube-apiyoutube-data-api

解决方案


答案很简单:根据Videos.updateAPI 端点的官方规范,part请求参数是必需的。

因此,您的调用Videos.update应如下所示:

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

注意添加的行part = "snippet"。这就是端点抱怨的原因。


推荐阅读