框访问被拒绝 - 权限不足 403 Python JWT sdk

时间:2021-04-29 21:56:27

标签: python jwt boxsdk

我正在尝试使用 Python 将文件上传到 Box。我已按照以下步骤操作:

  1. 使用 JWT 创建自定义应用
  2. 设置以下设置:
    • 在“配置”选项卡中,选择“应用访问级别”=“仅限应用访问”
    • 在“应用程序范围”下,我选中了“写入所有文件”框
    • 在“高级功能”下,我选中了“使用用户身份进行 API 调用”框
  3. 点击“生成公钥/私钥对”并将文件保存为 config.json
  4. 在管理控制台中使用客户端 ID 授权自定义应用程序。
  5. 运行此代码:https://github.com/asen123/box-python-sdk-large-file-upload/blob/master/upoad.py
from boxsdk import JWTAuth
from boxsdk import Client

# read json configuration file
auth = JWTAuth.from_settings_file('config.json')

access_token = auth.authenticate_instance()

# initialize sdk client
client = Client(auth)
service_account = client.user().get()
print('Service Account user ID is {0}'.format(service_account.id))

#file name and path
file_name = 'FILE_NAME'
stream = open('PATH_TO_FILE', 'rb')

#box parameters
folder_id = '0'
user_id = '0' 
user = client.user(user_id)

#make the call
box_file = client.as_user(user).folder(folder_id).upload_stream(stream, file_name)
print('File "{0}" uploaded to Box with file ID {1}'.format(box_file.name, box_file.id))

但是在倒数第二行,它抛出这个错误:

BoxAPIException: Message: Access denied - insufficient permission
Status: 403
Code: access_denied_insufficient_permissions
Request ID: vbqcplgq1cbpg5cj
Headers: {'Date': 'Thu, 29 Apr 2021 21:53:18 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Content-Length': '217', 'Connection': 'keep-alive', 'X-Envoy-Upstream-Service-Time': '100', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store'}
URL: https://upload.box.com/api/2.0/files/content
Method: POST
Context Info: None

我也尝试了 upload 而不是 upload_stream 并得到了相同的结果。有谁知道为什么我可能仍然没有足够的权限?

1 个答案:

答案 0 :(得分:0)

我认为您的问题在于:


user_id = '0'
user = client.user(user_id)

user_id 应该是有权访问 Box 中文件夹的应用程序用户帐户的 ID。

https://developer.box.com/reference/post-users/

我通常使用邮递员来创建帐户。


curl --location --request POST 'https://api.box.com/2.0/users' \
--header 'Authorization: Bearer {Your App Dev Token}' \
--header 'Content-Type: application/json' \
--data-raw '{"name": "{Name for New User}", "is_platform_access_only": true}'

响应将返回有关您刚刚创建的用户的一些信息,其中一部分是 ID。

{
  "id": 11446498,
  ...
  "name": "{Name for New User}",
  ...
}

将此新用户添加到您要上传到的文件夹中并使用该 ID 作为 as_user ID

在您的代码示例中。将 user_id 更新为返回的 ID。

user_id = '11446498'
user = client.user(user_id)