Skip to content

Make API calls as admin #547

@StefanMarchand

Description

@StefanMarchand

I'm trying to delete all files in the trash for each user of an enterprise if those files are older than 90 days. I'm a Box Co-Admin, I have all permission enabled as well as Global Content Manager enabled for my app and the app was refreshed after these permissions were set.

When I run my python script I am able to delete some files but not others with seemingly no differences in permissions between those files. I get error 403 access denied for those undeleted files. There are non legal holds on the undeleted files. I am also not able to go into that users account through the admin console and delete those files in the trash manually. I'll post my code below. I run my app with a developer token from my own account. Any help would be appreciated.

`#!/usr/bin/env python3

-- coding: utf-8 --

"""
Created on Wed Sep 9 18:14:19 2020

@author: stefanmarchand
"""
import csv
from boxsdk import DevelopmentClient
from datetime import datetime

startTime = datetime.now()
client = DevelopmentClient()
adminUser = client.user().get()
users = ('7286512266', '2650951631', '3590922200', '287093725', '9321039689')

#user = user_client.user().get()

todayDate = datetime.now()
deleteCnt = 0

with open('errorLog90.csv', 'w', newline='') as errLog:
writerErr = csv.writer(errLog)
with open('trashGreaterThan90Days.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Owner email", "file name", "trashed date"])

    for user in users:
        user_to_impersonate = client.user(user_id=user)
        user_client = client.as_user(user_to_impersonate)
        person = user_client.user().get()
        trashed_items = user_client.trash().get_items(limit=5000, fields=('trashed_at', 'name', 'created_by'))
        #try:
        print(person.name, person.type)
          
        for trashed_item in trashed_items:
            trashedDate = trashed_item.trashed_at[0:10]
            date_time_obj = datetime.strptime(trashedDate, '%Y-%m-%d')
            time_between_insertion = todayDate - date_time_obj
            
            if time_between_insertion.days>90:
                writer.writerow([person.login, trashed_item.name, trashed_item.trashed_at, person.id, trashed_item.id, trashed_item.created_by, person.type])
                
                try:
                    user_to_impersonate2 = client.user(user_id='13773158424')
                    user_client2 = client.as_user(user_to_impersonate2)
                    s = user_client2.user().get()
                    
                    s.trash().permanently_delete_item(trashed_item)
                    deleteCnt = deleteCnt + 1
                except:
                    writerErr.writerow([person.login, person.id, trashed_item.id, trashed_item.trashed_at, trashed_item.name])
                #put delete logic here
                
            del trashed_item   
            
        # except:
        #     writerErr.writerow(person.id)
        print('Deleted {0} items.'.format(deleteCnt))   
        deleteCnt = 0
        del trashed_items
        
file.close() 

errLog.close()
print(datetime.now() - startTime)`

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions