Joe*_*hew 1 python django python-3.x box boxsdk
我正在尝试使用 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))
Run Code Online (Sandbox Code Playgroud)
我已经使用 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"
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息是:
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))
Run Code Online (Sandbox Code Playgroud)
我承认我忽略了boxsdk文档的一部分,其中提到boxsdk的JWTAuth要求安装boxsdk并附加JWTAuth的依赖项。
因此,就我而言,对于本地计算机,我需要执行以下操作:
pip install boxsdk[jwt]
Run Code Online (Sandbox Code Playgroud)
对于heroku,我需要将以下内容添加到requirements.txt:
boxsdk[jwt]>=2.0.0
Run Code Online (Sandbox Code Playgroud)
一旦使用正确的依赖项安装了boxsdk,错误就消失了。线索在于模块的目录中如何缺少所需的方法。感谢 Box 开发团队的 Matt Willer 为我指明了正确的方向。
| 归档时间: |
|
| 查看次数: |
2786 次 |
| 最近记录: |