Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
Waleed Magdy
2 years agoExplorer | Level 4
Transfer Dropbox Backups using Dropbox APIs
Hello, Specifically, I have several backups stored in Dropbox Backup (some exceeding tens of terabytes), and I’m interested in utilizing the Dropbox API to automate the process of downloading th...
Waleed Magdy
2 years agoExplorer | Level 4
Thanks for your reply, however I tried many times but still only getting the folders and files in the root directory for the dropbox account but not the backups in dropbox backups
this is the script I am trying with if you can take a look:
import requests
import json
# Dropbox API credentials
DROPBOX_ACCESS_TOKEN = "TOKEN"
DROPBOX_REFRESH_TOKEN = (
"REFRESH_TOKEN"
)
APP_KEY = "APP_KEY"
APP_SECRET = "APP_SECRET"
# endpoints
DROPBOX_OAUTH2_TOKEN_URL = "https://api.dropbox.com/oauth2/token"
DROPBOX_LIST_FOLDER_URL = "https://api.dropboxapi.com/2/files/list_folder"
DROPBOX_TEAM_MEMBERS_URL = "https://api.dropboxapi.com/2/team/members/list"
# Refresh the access token using refresh token
def refresh_dropbox_token():
data = {
'grant_type': 'refresh_token',
'refresh_token': DROPBOX_REFRESH_TOKEN,
'client_id': APP_KEY,
'client_secret': APP_SECRET
}
response = requests.post(DROPBOX_OAUTH2_TOKEN_URL, data=data)
if response.status_code == 200:
tokens = response.json()
return tokens['access_token']
else:
print("Error refreshing token:", response.text)
return None
# This is to list team members to choose which team member to work with
def list_dropbox_team_members():
global DROPBOX_ACCESS_TOKEN
access_token = refresh_dropbox_token()
if access_token is None:
print("Failed to refresh Dropbox access token.")
return None
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
data = json.dumps({})
response = requests.post(DROPBOX_TEAM_MEMBERS_URL, headers=headers, data=data)
if response.status_code == 200:
members = response.json()
team_members = {}
for member in members['members']:
team_members[member['profile']['team_member_id']] = member['profile']['email']
print(f"User ID: {member['profile']['team_member_id']}, Email: {member['profile']['email']}")
return team_members
else:
print("Error listing team members:", response.text)
return None
# List Folders and Files in the Team member account
def list_dropbox_backups(selected_user_id):
global DROPBOX_ACCESS_TOKEN
access_token = refresh_dropbox_token()
if access_token is None:
print("Failed to refresh Dropbox access token.")
return
headers = {
'Authorization': f'Bearer {access_token}',
'Dropbox-API-Select-User': selected_user_id,
'Content-Type': 'application/json'
}
data = json.dumps({
"path": "",
"recursive": False
})
response = requests.post(DROPBOX_LIST_FOLDER_URL, headers=headers, data=data)
if response.status_code == 200:
files = response.json()
for entry in files['entries']:
print(f"Name: {entry['name']}, Type: {entry['.tag']}")
else:
print("Error listing files:", response.text)
if __name__ == "__main__":
team_members = list_dropbox_team_members()
if team_members:
selected_email = input("Enter the email of the team member whose backups you want to list: ")
selected_user_id = None
for user_id, email in team_members.items():
if email == selected_email:
selected_user_id = user_id
break
if selected_user_id:
print(f"Listing backups for user: {selected_email} (User ID: {selected_user_id})")
list_dropbox_backups(selected_user_id)
else:
print(f"No team member found with the email: {selected_email}")
Please let me know if you found anything to be changed, also I attached a screen shot here to be in the same page which service for the backups I am talking about.
Greg-DB
Dropbox Community Moderator
2 years agoLooking at this code, there are a few things to note:
- You're only calling /2/files/list_folder but not /2/files/list_folder/continue. You're not guaranteed to get everything back from just /2/files/list_folder; you need to check the returned has_more value and implement support for /2/files/list_folder/continue as well. Please check the linked documentation for more information.
- You're only checking the directly folder ("path": "") contents, and not any of its children ("recursive": False). You'd only get items directly in the folder but not anything any deeper. You'd need to call back again with the relevant subfolder, or otherwise use the recursive mode to get nested entries.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!