![](/img/trans.png)
[英]GAE: Can't Use Google Server Side API's (Whitelisting Issue)
[英]Flask on GAE - Connect to Google API - Can't use JSON file
我對 Flask、GAE 和 API 的使用相當陌生。我正在嘗試構建一個基本的 Web 應用程序,它可以連接到 Google 的 API 之一。
我的文件夾結構如下所示(我將其保存在主文件中):
應用程序網絡情緒:
-app.yaml
-main.py
-lib
--sentimentanalysis.py
-靜止的
--credential.json
一切正常,但為憑據提供 json 文件。 我的理解是有幾種方法可以做到這一點:
1) 將 GOOGLE_APPLICATION_CREDENTIALS 環境變量設置為 app.yaml 中我的文件的目標
2)通過我的腳本(sentimentanalysis.py)請求文件
不幸的是,我無法完成這些工作。
選項1):
在 app.yaml 我有一行:
env_variables:
GOOGLE_APPLICATION_CREDENTIALS: static/key/credentials.json
然后我通過 dev_appserver.py 運行我的代碼。 並得到以下錯誤:
ApplicationDefaultCredentialsError: File static/key/credentials.json (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!
選項 2):我的腳本 sentimentanalysis.py 中有一行代碼:
scope = ['https://www.googleapis.com/auth/cloud-platform']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/static/credentials.json', scope)
運行代碼時出現以下錯誤:
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/static/credentials.json'
INFO 2016-08-06 04:10:51,678 module.py:788] default: "POST /Sentiment-analysis HTTP/1.1" 500 -
問題:
所以看起來無論我使用哪種方法,我都無法提供 JSON 文件的正確路徑
我的問題是首先要知道上述任何選項是否正確,如果是,我做錯了什么? 如果它們不是正確的選擇,您會推薦什么?
抱歉,如果已經有人問過這個問題,我已經嘗試了幾個小時來尋找答案,但一直沒能破解它……
謝謝!
如果您在Google App Engine上運行,那么您的代碼會自動獲得所需的憑據。 請勿設置GOOGLE_APPLICATION_CREDENTIALS,也不要調用.from_json_keyfile_name。 相反,請致電:
credentials = GoogleCredentials.get_application_default()
如下所示: https : //github.com/GoogleCloudPlatform/python-docs-samples/blob/master/bigquery/api/getting_started.py
設置 GOOGLE_APPLICATION_CREDENTIALS=credentials.json 如果您使用的是 cmd,請使用此命令
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.