Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save KyMidd/ed7cbc4b4c11cd7a8c96de2953b6859f 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 files are shared in a Teams chat, they are stored in the user's OneDrive under "Microsoft Teams Chat Files"
# This is the method most often used for 1:1 chats or group chats
try:
base_path = f"Microsoft Teams Chat Files/{file_name}"
encoded_path = urllib.parse.quote(base_path, safe="/")
graph_url = f"https://graph.microsoft.com/v1.0/me/drive/root:/{encoded_path}:/content"
response = requests.get(graph_url, headers=headers)
if response.status_code == 200:
print(f"🟢 Download succeeded from user's OneDrive: {graph_url}")
return response.content
else:
print(f"🚫 OneDrive download failed: {response.status_code} - {response.text}")
except Exception as e:
print(f"🚫 OneDrive download error: {str(e)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment