Python Google API客户端调用Youtube Data API返回bound method修复
问题原因
代码中response = request.execute这行没有实际执行接口请求逻辑:你只是把execute方法本身的引用赋值给了response变量,并没有调用这个方法,因此打印时输出的是绑定方法的对象内存描述,拿不到接口返回的JSON结果。
修复方案
只需要给execute加上括号,触发方法实际执行即可,execute()方法调用完成后会直接返回和接口JSON结构对应的Python字典对象。
修复后的完整可运行代码如下:
from googleapiclient.discovery import build import json api_key = '<my_api_key_redacted>' youtube = build('youtube', 'v3', developerKey=api_key) request = youtube.channels().list(part='statistics', forUsername='<my_user_id_redacted>') # 核心修复:给execute加(),实际发起请求获取响应 response = request.execute() # 直接打印得到字典格式的响应 print(response) # 如果需要格式化输出易读的JSON结构,可以用json模块做缩进处理 print(json.dumps(response, indent=2, ensure_ascii=False))
内容的提问来源于stack exchange,提问作者Devin Martin
相关产品推荐
相关产品推荐

