簡體   English   中英

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

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

我正在嘗試使用 Python 將文件上傳到 Box。 我已按照以下步驟操作:

  1. 使用 JWT 創建自定義應用程序
  2. 設置以下設置:
    • 在“配置”選項卡中,select“應用程序訪問級別”=“僅應用程序訪問”
    • 在“應用程序范圍”下,我選中了“寫入所有文件”框
    • 在“高級功能”下,我選中了“使用用戶標頭進行 API 調用”框
  3. 單擊“生成公鑰/私鑰對”並將文件保存為 config.json
  4. 在 Admin Console 中使用客戶端 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並得到了相同的結果。 有誰知道為什么我可能仍然沒有足夠的權限?

我認為您的問題在於:


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

user_id應該是有權訪問 Box 中文件夾的應用用戶帳戶的 ID。

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

要創建帳戶,我通常使用 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}'

響應將返回有關您剛剛創建的用戶的一些信息,其中一部分是 ID。

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

將此新用戶添加到您要上傳到的文件夾中,並將 ID 用作as_user ID

在您的代碼示例中。 將 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