我才刚刚开始学习编程(和python,flask),我有一个情况,我不能自己解决(或使用谷歌)。
我正在尝试通过youtube data v3访问我的youtube频道数据,并使用google官方库进行oauth和数据检索。
下面的代码就是我使用的代码。该方法的返回运行良好,但是它只在我的浏览器中呈现json响应,我不能‘捕获’并处理它。
@app.route('/')
def index():
if 'credentials' not in flask.session:
return flask.redirect('authorize')
credentials = google.oauth2.credentials.Credentials(
**flask.session['credentials'])
client = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials)
return channels_list_by_username(client,
part='snippet,contentDetails,statistics',
forUsername='username')我想把调用放在一个新的变量中,json.dump()-ing响应,并像这样访问它,但它就是不起作用。
在这方面的任何帮助都是非常感谢的。谢谢!
发布于 2019-01-08 03:25:30
您可以将该调用的结果存储到一个变量中,对其进行处理,然后返回
@app.route('/')
def index():
if 'credentials' not in flask.session:
return flask.redirect('authorize')
credentials = google.oauth2.credentials.Credentials(
**flask.session['credentials'])
client = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials)
# Here is your variable
channels = channels_list_by_username(client,
part='snippet,contentDetails,statistics',
forUsername='username')
# Process it
print(channels)
# Add other processing stuff here
# Return it
return channels发布于 2019-01-08 03:29:11
看过youtube API的源代码后,我想我知道你在问什么了。
channels_list_by_username() print将一些信息发送到标准输出。你不想打印它,你想要实际的数据,以便处理它,对吗?快速浏览一下该API,我猜您可以尝试如下所示:
...
client = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials)
results = client.channels().list(
part='snippet,contentDetails,statistics',
forUsername='username'
).execute()
... process data here ...
return results看起来您的代码来自youtube API示例,该示例仅用于展示如何实现API。查看实际文档,了解如何获取原始数据。
https://stackoverflow.com/questions/54080493
复制相似问题