简体   繁体   English

Box Access denied - 权限不足 403 Python JWT sdk

[英]Box Access denied - insufficient permission 403 Python JWT sdk

I am trying to upload a file to Box using Python.我正在尝试使用 Python 将文件上传到 Box。 I have followed these steps:我已按照以下步骤操作:

  1. Create a custom app with JWT使用 JWT 创建自定义应用程序
  2. Set up the following settings:设置以下设置:
    • In "Configuration" tab, select "App Access Level" = "App Access Only"在“配置”选项卡中,select“应用程序访问级别”=“仅应用程序访问”
    • Under "Application Scopes", I checked the box for "write all files"在“应用程序范围”下,我选中了“写入所有文件”框
    • Under "Advanced Features", I checked the box for "Make API calls using the as-user header"在“高级功能”下,我选中了“使用用户标头进行 API 调用”框
  3. Click "Generate a Public/Private Key pair" and saved the file as config.json单击“生成公钥/私钥对”并将文件保存为 config.json
  4. Authorize the custom application using the Client ID at Admin Console.在 Admin Console 中使用客户端 ID 授权自定义应用程序。
  5. Run this code: https://github.com/asen123/box-python-sdk-large-file-upload/blob/master/upoad.py运行此代码: 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))

but in the second to last line, it throws this error:但在倒数第二行,它抛出了这个错误:

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

I also tried upload rather than upload_stream and got the same result.我也尝试了upload而不是upload_stream并得到了相同的结果。 Does anyone know why I may still have insufficient permission?有谁知道为什么我可能仍然没有足够的权限?

I think your issue is with:我认为您的问题在于:


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

The user_id should be the ID of the App User account that has access to the folder in Box. user_id应该是有权访问 Box 中文件夹的应用用户帐户的 ID。

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

To create the account I usually use postman.要创建帐户,我通常使用 postman。


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}'

The Response will return some information about the user you just created, one part being the ID.响应将返回有关您刚刚创建的用户的一些信息,其中一部分是 ID。

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

Add this new user to the folder you want to upload to and use the ID as the as_user ID将此新用户添加到您要上传到的文件夹中,并将 ID 用作as_user ID

In your code example.在您的代码示例中。 Update the user_id to be the ID returned.将 user_id 更新为返回的 ID。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM