【发布时间】: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 视频的标题。
【问题讨论】:
标签: python youtube-api youtube-data-api