Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created August 4, 2025 01:42
Show Gist options
  • Select an option

  • Save KyMidd/e400ecba0838ecff562682e617a99e12 to your computer and use it in GitHub Desktop.

Select an option

Save KyMidd/e400ecba0838ecff562682e617a99e12 to your computer and use it in GitHub Desktop.
def download_file_for_user(file_name, file_url, headers):
# ...
try:
if ".sharepoint.com/sites/" in file_url:
# ...
# If that doesn't work, do naive search of all files in the Teams sharepoint site
# If matching file found, download it
# This is a slow hack, but is the only method reliably working to access Teams/SharePoint hosted files
search_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/search(q='{urllib.parse.quote(file_name)}')"
print(f"🟢 Attempting fallback file search: {search_url}")
search_response = requests.get(search_url, headers=headers)
if search_response.status_code == 200:
for item in search_response.json().get("value", []):
if item.get("name") == file_name:
item_id = item["id"]
download_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{item_id}/content"
file_response = requests.get(download_url, headers=headers)
if file_response.status_code == 200:
print(f"🟢 Download succeeded from SharePoint search by file name: {download_url}")
return file_response.content
else:
print(f"🚫 SharePoint item download failed: {file_response.status_code} - {file_response.text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment