我有一个应用程序偶然使用公共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:5000
(Gunicorn
)
$ 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;
}
?
答案 0 :(得分:1)
在Flask
app.py
代码中设置凭据有效,如下所示:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_.json_credential_file"