【问题标题】:Define 'api' traceback errors in Python在 Python 中定义“api”回溯错误
【发布时间】:2021-07-14 21:07:11
【问题描述】:

我正在尝试使用 Python 中的 YouTube v3 API 并根据代码中的要求在 YouTube 上搜索视频,但出现此错误。

如何防止这种情况发生?这段代码也来自谷歌。您可以在 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()

第二个错误

【问题讨论】:

    标签: python python-3.x google-api youtube-data-api google-api-python-client


    【解决方案1】:

    是的,您的代码可能来自谷歌,但您已经重命名了一些东西。

    您的所有呼叫都需要通过 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

    您需要检查您发送的参数删除不是有效位置的位置

    【讨论】:

    • 是的,我发现我很笨,在第33行添加了API部分,但是当我运行它并获得授权码时,它就出现了这个。我之前也遇到过这个错误。 2nd error
    • 您发送的位置无效。
    • 天哪,我忘了读所有这些!非常感谢你的帮助。下次我会阅读更多内容。
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多