我正在尝试使用 django 使用 boxsdk 将文件上传到 box.com,同时将应用程序部署到 heroku。问题是我的代码在本地开发服务器上运行良好,但在 heroku 上却不行。如果应用程序在本地运行,它也可以使用heroku local web
对于本地开发服务器,我可以导入一个 json 文件进行身份验证。对于 heroku,由于它不接受 conf 文件,我使用 heroku:config 将文件存储为环境变量。
from boxsdk import JWTAuth, Client
from io import StringIO
import os
jsonpath = f'{STATIC_ROOT}/conf/box.json'
try:
auth = JWTAuth.from_settings_file(jsonpath)
except:
BOXCONF = os.environ.get('BOXCONF')
msg = f'The value of BOXCONF is {BOXCONF}'
capture_message(msg)
auth = JWTAuth.from_settings_file(StringIO.new(BOXCONF))
client = Client(auth)
service_account = client.user().get()
print('Service Account user ID is {0}'.format(service_account.id))
我已经通过使用 Sentry 的 capture_message 测试了 BOXCONF 已设置,它显示以下内容:
The value of BOXCONF is {
"boxAppSettings": {
"clientID": "abcd",
"clientSecret": "abc",
"appAuth": {
"publicKeyID": "xyz",
"privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----\nblabla\n-----END ENCRYPTED PRIVATE KEY-----\n",
"passphrase": "1234"
}
},
"enterpriseID": "1234"
}
我收到的错误信息是:
AttributeError: 'NoneType' object has no attribute 'from_settings_file'
File "django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "app/views.py", line 281, in fileupload
link = handle_uploaded_file(request.FILES['docfile'], sectionchoice, sectiontext, sectiondescription, filename, filedescription)
File "app/views.py", line 353, in handle_uploaded_file
auth = JWTAuth.from_settings_file(StringIO.new(BOXCONF))