使用Flask和Gunicorn设置Google凭据

时间:2018-01-06 04:39:14

标签: python flask gunicorn youtube-data-api

我有一个应用程序偶然使用公共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 运行。

现在我正在浏览器上测试应用程序。当我使用Flask Listening at: 127.0.0.1:5000Gunicorn

通过$ gunicorn app:app --workers 16运行Listening at: 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
然而,

GOOGLE_APPLICATION_CREDENTIALS在同一条道路上。

如何在Flask内正确设置 public static String phrasePicker()throws IOException{ Scanner input = new Scanner(new File ("Phrases.txt")); String [] phrases = new String [140]; int i = 0; //checks if there is a next line in the text file while(input.hasNext()) { //if there is a text file, put that line as the index "i" of the array phrases[i] = input.nextLine(); i= i++; } Random random = new Random(); int select = random.nextInt(phrases.length); String phrase = phrases[select]; return phrase; }

1 个答案:

答案 0 :(得分:1)

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

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