【发布时间】:2021-04-25 22:53:11
【问题描述】:
在将 JSON 分配给像 sheet_id = data["sheetID"] 这样的 python 变量时,我收到错误 TypeError: 'NoneType' object is not subscriptable。
它只发生在我的 Google App Engine 实例上。在本地运行我的 Flask 应用程序并使用 Postman 向应用程序发送 POST 请求时,我没有得到它。这是代码sn-p
@app.route('/main',methods=["POST"])
def main():
data = request.json
print(data)
scope = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/gmail.send',
'https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/gmail.settings.sharing'
]
GCP_PROJECT = os.environ['GCP_PROJECT']
GAE_SID_VERSION = os.environ['GAE_SID_VERSION']
credentials1 = access_secret_version(GCP_PROJECT, <SECRET NAME>, GAE_SID_VERSION)
credentials1 = json.loads(credentials1)
credentials = service_account.Credentials.from_service_account_info(credentials1)
scoped_credentials = credentials.with_scopes(scope)
gc = gspread.authorize(scoped_credentials)
#for sheet with categories and score
sheet_id = data["sheetID"] #this gives me the error
workbook = gc.open_by_key(sheet_id)
...
...
return "OK"
奇怪的是整个程序运行完成,我收到response = OK。我可以将 json 数据从 Google 表格中的触发脚本发送到我的 GAE 实例,然后 GAE 实例稍后将数据加载到 Gcloud 存储桶中。我从this link 了解到data 或sheet_id 可能是None,但在这种情况下,我的整个程序将如何运行完成甚至只是打开工作簿?这是 Google Apps 脚本
function runDockerImage(sheetID , mainWorksheet , emailTo , emailList , deliverToCloud ,filterOn , labelDisplay , sandbox , deliverToSlack) {
// The code below logs the HTML code of the Google home page.
var dataJSON = {
'sheetID' :sheetID,
'mainWorksheet' : mainWorksheet,
'emailTo' : emailTo,
'emailList' : emailList,
'deliverToCloud' : deliverToCloud,
'filterOn' : filterOn,
'labelDisplay': labelDisplay,
'sandbox' : sandbox,
'deliverToSlack' : deliverToSlack
};
payload = JSON.stringify(dataJSON)
var headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic _authcode_'
};
var options = {
'method': 'post',
'contentType': 'application/json',
'headers': headers,
'payload': payload
};
var response = UrlFetchApp.fetch('https://<project-id>.uc.r.appspot.com/main', options);
Logger.log(response);
}
这是来自 App Engine 错误报告日志的追溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/app.py", line 118, in main
sheet_id = data["sheetID"]
TypeError: 'NoneType' object is not subscriptable
最后,我想问一下,这个程序怎么可能完全运行并出现这个错误?我想如果我不解决这个问题,我以后可能会遇到一些严重的问题。
错误报告日志的屏幕截图
【问题讨论】:
标签: python json google-app-engine flask google-cloud-platform