python - 为什么 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 视频的标题。
解决方案
答案很简单:根据Videos.update
API 端点的官方规范,part
请求参数是必需的。
因此,您的调用Videos.update
应如下所示:
request = youtube.videos().update(
part = "snippet",
body = {
"id": "OSxK-tscmVA",
"snippet": {
"title":"It's Changed!",
}
}
)
注意添加的行part = "snippet"
。这就是端点抱怨的原因。
推荐阅读
- websocket - MQTT 用于聊天应用程序中的客户端通信
- python - 如何在不停止代码的情况下继续在 python 中请求有效输入
- android - 在指纹 API 中向后兼容使用 - Android Studio
- c# - USRP B200 C# API
- javascript - express和sequelize中同一路由上的多个get方法冲突
- ios - AppTrackingTransparency - 权限已添加但仍面临错误
- flutter - 我无法选中/取消选中复选框(颤振)
- windows - Symfony4 在 Windows 上运行缓慢 - 在 $kernel->terminate($request, $response); 之后会发生什么?
- c++ - 如何使用 libopenssl 重放加密流量?
- haproxy - 如何限制 HAProxy 只提供特定的 URI?