【问题标题】:Setting Google Credentials with Flask and Gunicorn使用 Flask 和 Gunicorn 设置 Google 凭据
【发布时间】:2018-06-15 21:33:19
【问题描述】:

我有一个应用程序偶然使用了公共 youtube API 数据,而不是与用户相关的数据。

第 1 阶段从命令行运行

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

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

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

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json

api 调用成功,一切正常。

第 2 阶段使用 Flask 和 Gunicorn 运行

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

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

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

【问题讨论】:

    标签: python flask gunicorn youtube-data-api


    【解决方案1】:

    Flask app.py 代码中设置凭据,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2016-05-06
      • 1970-01-01
      • 2013-01-12
      • 2014-09-14
      • 2018-08-03
      • 1970-01-01
      相关资源
      最近更新 更多