我有一个应用程序偶然使用了公共(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/