为啥 YouTube 数据 API 中存在缺失部分错误
Posted
技术标签:
【中文标题】为啥 YouTube 数据 API 中存在缺失部分错误【英文标题】:Why there's a missing part error in YouTube Data API为什么 YouTube 数据 API 中存在缺失部分错误 【发布时间】:2021-10-15 03:57:58 【问题描述】:为什么 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"
Main.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():
# 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 视频的标题。
【问题讨论】:
【参考方案1】:答案很简单:根据Videos.update
API 端点的官方规范,part
请求parameter is required。
因此,您对Videos.update
的调用应如下所示:
request = youtube.videos().update(
part = "snippet",
body =
"id": "OSxK-tscmVA",
"snippet":
"title":"It's Changed!",
)
注意添加的行part = "snippet"
。这就是端点抱怨的原因。
【讨论】:
您要求添加的 `part = "sn-p"' 给我带来了另一个错误。错误对于评论来说太长了。你可以找到它here @Knight_Crawler:我在上面向您展示的修复是官方规范要求的。此代码更改修复了您的错误Missing required parameter "part"
。即使我在Videos.list
的官方规范上方链接,您似乎也不倾向于使用它:您调用端点的request body 也需要指定视频类别ID才能被接受通过 API 后端。
@Knight_Crawler:请注意,来自 Google 的 update_video.py
示例代码包含所有这些内容。我从一开始就警告您,您必须吸收(即理解)该代码。除了仔细阅读官方文档,别无他法。以上是关于为啥 YouTube 数据 API 中存在缺失部分错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Youtube/Google API 不返回超过 5 个回复?
IOS - 使用 Youtube API 检查 Youtube 视频是不是存在