python - 使用 Flask 和 Gunicorn 设置 Google 凭据

标签 python flask gunicorn youtube-data-api

我有一个应用程序偶然使用了公共(public) youtube API 数据,与用户无关。

STAGE 1: Running from command line

在我的 youtube_module.py 顶部,我有:

from oauth2client.client import GoogleCredentials
CREDENTIALS = GoogleCredentials.get_application_default() 

根据文档要求,在 .bashrc 我设置:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json

API 调用成功,一切正常。

STAGE 2: Running with Flask and Gunicorn.

现在我正在浏览器上测试该应用程序。当我通过 Gunicorn 使用 $ Gunicorn app:app 运行 Flask 应用程序(Listening at: 127.0.0.1:5000)时 -- worker 16(收听:http://127.0.0.1:8000)

应用程序提供了几个页面,但随后遇到以下错误:

ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. 
然而,

credentials.json 位于同一路径上。

如何在 Flask 中正确设置 GOOGLE_APPLICATION_CREDENTIALS

最佳答案

Flask app.py 代码中设置凭据可以正常工作,如下所示:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_.json_credential_file"

关于python - 使用 Flask 和 Gunicorn 设置 Google 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124312/

相关文章:

python - 除了标准库中包含的工具之外,Linux 上的 Python 还有哪些分析工具?

python - flask-sqlalchemy 列的最大值

python - Heroku 本地失败,没有错误消息

python - 使用 Python Flask 流式传输数据时为 "AssertionError: applications must write bytes"

python - 如何在 google Colab 中包含 MSRA 10K 数据集?

python - 计算 3D 锯齿状 NumPy 数组的 2D 均值

python - 使用 Selenium 登录网站,但继续处理(登录时)请求

python - AirBnB 知识库 : Deploying using a context route

Python、Flask、Gunicorn 错误 : Unrecognized Arguments

django - 尽管ProxyPassReverse,通过mod_proxy产生的gunicorn仍在项目范围之外进行重定向