在 Python 中定义 'api' 回溯错误
Define 'api' traceback errors in Python
我正在尝试使用 Python 中的 YouTube v3 API 根据代码中的要求在 YouTube 上搜索视频,但出现此错误。
如何防止这种情况发生?此代码也来自 Google。您可以在 Google 网站 here 上编辑代码。我在这方面完全是个菜鸟,所以如果你有任何我以后必须知道的信息,请告诉我。谢谢
# -*- coding: utf-8 -*-
# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = api.youtube.search().list(
part="snippet",
location="United States",
maxResults=5,
order="date",
q="programming",
regionCode="US",
relevanceLanguage="en",
safeSearch="none",
videoDimension="any",
videoDuration="any"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
错误二
是的,您的代码可能来自 google,但您重命名了一些内容。
您的所有调用都需要通过 YouTube 服务对象
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
但您正在使用 api
调用它
request = api.youtube.search().list(
应该是
request = youtube.search().list(
再次检查 google 中的代码,如果您不明白这些代码的用途,那么在重命名时会变得很麻烦。
# -*- coding: utf-8 -*-
# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.search().list(
part="snippet",
location="United States",
maxResults=5,
order="date",
q="Programming",
regionCode="US",
relevanceLanguage="en",
safeSearch="none",
videoDimension="any",
videoDuration="any"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
错误编号 2
您需要检查发送的参数,删除无效位置的位置
我正在尝试使用 Python 中的 YouTube v3 API 根据代码中的要求在 YouTube 上搜索视频,但出现此错误。
如何防止这种情况发生?此代码也来自 Google。您可以在 Google 网站 here 上编辑代码。我在这方面完全是个菜鸟,所以如果你有任何我以后必须知道的信息,请告诉我。谢谢
# -*- coding: utf-8 -*-
# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = api.youtube.search().list(
part="snippet",
location="United States",
maxResults=5,
order="date",
q="programming",
regionCode="US",
relevanceLanguage="en",
safeSearch="none",
videoDimension="any",
videoDuration="any"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
错误二
是的,您的代码可能来自 google,但您重命名了一些内容。
您的所有调用都需要通过 YouTube 服务对象
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
但您正在使用 api
调用它request = api.youtube.search().list(
应该是
request = youtube.search().list(
再次检查 google 中的代码,如果您不明白这些代码的用途,那么在重命名时会变得很麻烦。
# -*- coding: utf-8 -*-
# Sample Python code for youtube.search.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.search().list(
part="snippet",
location="United States",
maxResults=5,
order="date",
q="Programming",
regionCode="US",
relevanceLanguage="en",
safeSearch="none",
videoDimension="any",
videoDuration="any"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
错误编号 2
您需要检查发送的参数,删除无效位置的位置