Skip to main content

Hello, I have a question about the box API when OAuth 2.0 is used. My end goal is to upload a file to box using a bot. The first part of my test is to set up the API and do this manually for now.


With this goal in mind, I created a personal box account (my organization suggested doing this) to do testing. Then, I created an application and followed the instructions from box to authenticate using Python and SDK.


The problem that I’m facing is that I cannot view my root folder. I get a message saying <Box Folder - 0 (All Files)>. I can create folders, search for files, and upload file to box root folder.


I read some of the posts on the forum and I already discarded the possibility that I am using a service account because when I look at my client.user() i see my user ID and email. Also, I’m using a developer token and to my understanding, a developer token is associated with the user who’s logged into the developer console. The code that I’m using is below, thank you!



from boxsdk import OAuth2, Client

auth = OAuth2(
client_id='MY_CLIENT_ID...',
client_secret='MY_CLIENT_SECRET...,
access_token='rC2TetewQsKzZxCz2gVdM0VbLDcD1NcZ'
)
auth_url, csrf_token = auth.get_authorization_url('https://developer.box.com/auth/callback')

# redirect(auth_url, code=302)
client = Client(auth)

#I get my user
user = client.user().get()
print(f'\n\nThe current user is \n{user}\n\n')

#can not get the root folder
root_folder = client.root_folder().get()
print("the root folder is ",root_folder)

#But I can send files to the root folder, create folders, do query searches etc
folder_id = '0'
new_file = client.folder(folder_id).upload('test.pdf')
print(f'File "{new_file.name}" uploaded to Box with file ID {new_file.id}')

Welcome to the Forum, @Sahachel24


What are you trying to do with your root folder exactly ?

For all customers, the root folder ID is always = to 0

There is no specific folder ID for the root folder.


Hi @Sahachel24 ,


I’ve tried your code with a couple variations and they all work just fine.


If I understood correctly, you’re trying to read the content of a folder, specifically the root folder. That is done with the folder.get_items()method. The folder.get() method returns the folder metadata it self.


For example based in your code:


from boxsdk import OAuth2, Client

auth = OAuth2(
client_id='by...hm',
client_secret='ad...Vjr',
access_token='Tx...Db'
)
# auth_url, csrf_token = auth.get_authorization_url('https://developer.box.com/auth/callback')

# redirect(auth_url, code=302)
client = Client(auth)

# I get my user
user = client.user().get()
print(f'\n\nThe current user is \n{user}\n\n')

# can not get the root folder
root_folder = client.root_folder().get()
# this also works
# root_folder = client.folder('0').get()
print("the root folder is ", root_folder)


# But I can send files to the root folder, create folders, do query searches etc
# folder_id = '0'
# new_file = client.folder(folder_id).upload('test.pdf')
# print(f'File "{new_file.name}" uploaded to Box with file ID {new_file.id}')

# list items in root folder
# this also works
# items = client.folder(folder_id='0').get_items(limit=100, offset=0)
items = root_folder.get_items()
for item in items:
print(f"{item.type} {item.id} {item.name}")


and in my case this script returns:


The current user is
<Box User - 25428698627 (Free Dev 001)>


the root folder is <Box Folder - 0 (All Files)>
folder 234102715058 My Sign Requests
folder 216797257531 My Signed Documents
folder 221723756896 UIE Samples
folder 223095001439 workshops
file 1204688948039 Get Started with Box.pdf
file 1351892000645 sample_file_copy.txt

Let me give you a couple of resources that you might find interesting:


We also have a sample app template for the classic SDK, which will trigger the oAuth flow, you can take a look here:



This to avoid having to re-generate the developer token every 60 minutes.


We also have a couple of workshops.


For the classic SDK:



For the new Gen SDK



Both have sample apps on different topics.


Let us know if this helps.


Cheers


Reply