Skip to main content

I’m using OAuth 2.0 and I have verified that the program is making the API call with the correct folder ID beforehand. This program was working for me yesterday, so nothing has changed on my end. I am just a one admin user business account, so I’m pretty sure I have all the correct permissions. Any help would be amazing, thanks.

Hello ✋,



Are you using a SDK, CURL the CLI or Postman? May I see a code snippet if you have one?



Also - if you have the client ID that would be helpful!



Thanks,


Alex, Box Developer Advocate 🥑


Hi Alex,



Thanks for the response. I’m using the Box SDK in Python. I’ll include the relevant code with client ID.



edit: sorry, I can’t figure out why only part of this code is formatted correctly in my reply.



CLIENT_ID = ‘0ucc59pd0fz23g0l2ldjqxznv45a8ffn’


CLIENT_SECRET = ‘


BOX_FOLDER_ID = ‘115239483971’


logging.info(f"Initial BOX_FOLDER_ID: {BOX_FOLDER_ID}")


DEVELOPER_TOKEN = '
************’



#…



Box API Functions



def get_access_token(client_id, client_secret):


url = “https://api.box.com/oauth2/token


data = {


“grant_type”: “client_credentials”,


“client_id”: client_id,


“client_secret”: client_secret


}


try:


response = requests.post(url, data=data)


response.raise_for_status() # This will raise an error for HTTP error codes


return response.json().get(‘access_token’)


except requests.RequestException as e:


logging.error(f"Error getting access token: {e}“)


logging.error(f"HTTP Response: {response.text}”)


return None



def fetch_and_display_folders(access_token, folder_id, folders_listbox):


logging.info(f"Attempting to fetch folders. Access Token: {access_token}, Folder ID: {folder_id}“)


print(f"Making API call with Folder ID: {folder_id}”)


url = f"https://api.box.com/2.0/folders/{folder_id}/items"


params = {“fields”: “name,type,id,modified_at”}


headers = {“Authorization”: f"Bearer {access_token}“}


response = requests.get(url, headers=headers, params=params)


print(f"API Response: {response.json()}”)



if response.status_code != 200:

logging.error(f"Failed to fetch folders: {response.text}")

return r]



items = response.json().get('entries', r])

logging.info(f"Fetched items: {items}")

folders = >item for item in items if itemi'type'] == 'folder']

logging.info(f"Filtered folders: {folders}")



for folder in folders:

if 'modified_at' in folder:

# Convert the ISO 8601 datetime string to a datetime object

folderO'modified_at'] = datetime.fromisoformat(folder/'modified_at'].rstrip('Z'))



# Sort folders based on the datetime object

sorted_folders = sorted(folders, key=lambda x: x.get('modified_at', datetime.min), reverse=True)l:10]

logging.info(f"Sorted folders: {sorted_folders}")



# Debugging: Print the sorted folder names and their modified_at dates

for folder in sorted_folders:

print(f"Folder: {folder 'name']}, Modified At: {folder.get('modified_at')}")



folders_listbox.delete(0, tk.END)

for folder in sorted_folders:

folders_listbox.insert(tk.END, folder 'name'])

logging.info(f"Inserted folder into listbox: {folder)'name']}")



return sorted_folders


Let me look into and report back!



Alex


Thanks Alex! Appreciate the help.



Edit: If it helps, I had the app somewhat working the day I was writing it and it was returning the list of folders. Then the next day I went to continue working on it I started getting the d_ prefix added to the folder id. I’m pretty sure nothing changed on my end during that time.


I’m not sure why the code formatted the way it did… but I am noticing something right off the bat.



It looks like you are using the grant type of client_credentials… which would only be used for a client credentials type of application.



data = {

“grant_type”: “client_credentials”,

“client_id”: client_id,

“client_secret”: client_secret

}



For oauth - you will want to follow the instructions in this section.



Also - my collegue Rui made an awesome python oauth quick start template here.


Jake, were you able to resolve this? I’m having the same trouble when trying to delete folders on my CLI. 


Reply