[...]
< < < < < < < < < <Сначала проверьте конфигурацию приложения в консоли разработчика > вкладка > ваше приложение > конфигурации и убедитесь, что:
- В разделе App Access Level у вас есть App+Enterprise Access .
- В разделе Дополнительные возможности выберите Делать вызовы API, используя заголовок as-user
- Сохранить изменения
- На вкладке Авторизация повторно авторизуйте ваше приложение, если это необходимо.
Теперь вы можете попробовать этот кусок кода:
"""quick demo to list items in folder while impersonating a user"""
from boxsdk import JWTAuth, Client
def main():
"""main function"""
auth = JWTAuth.from_settings_file('./.jwt.config.json')
auth.authenticate_instance()
client = Client(auth)
me = client.user().get()
print(f"Service account user: {me.id}:{me.name}")
user_id_to_impersonate = '18622116055'
folder_of_user_to_impersonate = '0'
user_to_impersonate = client.user(user_id=user_id_to_impersonate).get()
# the .get() is just to be able to print the impersonated user
print(f"User to impersonate: {user_to_impersonate.id}:{user_to_impersonate.name}")
user_client = client.as_user(user_to_impersonate)
items = user_client.folder(folder_id=folder_of_user_to_impersonate).get_items()
print(f"Items in folder:{items}")
# we need a loop to actually get the items info
for item in items:
print(f"Item: {item.type}\t{item.id}\t{item.name}")
Результат:
Service account user: 20344589936:UI-Elements-Sample
User to impersonate: 18622116055:Rui Barbosa
Item: folder 172759373899 Barduino User Folder
Item: folder 172599089223 Bookings
Item: folder 162833533610 Box Reports
Item: folder 163422716106 Box UI Elements Demo
Item: folder 191176042455 Bulk Upload
Item: folder 189803765719 ClassificationService