
In this tutorial, you will learn how to upload files and images to Microsoft OneDrive using its file storage API and Python. The complete source code of the Python OneDrive file upload script is given below.
Important Note: Replace the client_id and client_secret with your real OneDrive API client_id and client_secret. Also, update the URLs with your Tenant domain name.
import requests
import json
directory = r"c:\temp\uploads"
data = {'grant_type':"client_credentials",
'resource':"https://graph.microsoft.com",
'client_id':'XXXXX',
'client_secret':'XXXXX'}
URL = "https://login.windows.net/YOURTENANTDOMAINNAME/oauth2/token?api-version=1.0"
r = requests.post(url = URL, data = data)
j = json.loads(r.text)
TOKEN = j["access_token"]
URL = "https://graph.microsoft.com/v1.0/users/YOURONEDRIVEUSERNAME/drive/root:/fotos/HouseHistory"
headers={'Authorization': "Bearer " + TOKEN}
r = requests.get(URL, headers=headers)
j = json.loads(r.text)
print("Uploading file(s) to "+URL)
for root, dirs, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root,filename)
print("Uploading "+filename+"....")
fileHandle = open(filepath, 'rb')
r = requests.put(URL+"/"+filename+":/content", data=fileHandle, headers=headers)
fileHandle.close()
if r.status_code == 200 or r.status_code == 201:
#remove folder contents
print("succeeded, removing original file...")
os.remove(os.path.join(root, filename))
print("Script completed")
raise SystemExitGoogle Chrome has dominated web browsing for over a decade with 71.77% global market share.…
Perplexity just made its AI-powered browser, Comet, completely free for everyone on October 2, 2025.…
You've probably heard about ChatGPT Atlas, OpenAI's new AI-powered browser that launched on October 21,…
Perplexity Comet became free for everyone on October 2, 2025, bringing research-focused AI browsing to…
ChatGPT Atlas launched on October 21, 2025, but it's only available on macOS. If you're…
Two AI browsers just entered the ring in October 2025, and they're both fighting for…