
This code will download files into a "downloads" project subfolder, and preserve the folder structure you have on OneDrive. This code should provide a jumping-off point for, say, downloading just the images, or videos, or files with particular attributes. Then choose option 3 (to list all files and create file_list.json on your local machine), then option 4 (to incrementally fetch all the assets listed in file_list.json.) Choose option 1 to get your initial access_token and refresh_token saved locally to your machine.Create your own "App Registration" with the File.ReadWriteAll permission scope in Azure Portal (this is free.) This gives you two important variables that you need to set as environment variables to run this code.Once the file_list.json file is generated, this walks through that file and downloads the items, preserving the file structure as seen on OneDrive Various ways to get the access_token and refresh_token for the Microsoft opengraphĬode to generate the list of files and folders, to walk the OneDrive folder tree basically The loader application the command-line interface.

These steps give you the essential access_token and refresh_token.

All you're really looking for is the magical "code=" parameter in the address bar, and you copy it and paste the value which follows "code=" into the CLI when prompted.įrom there, the CLI happily saves this for you, and you're on your way. So you get an error message in the web page. It spins up a web browser to make a request for permission, you grant that permission, and Microsoft's endpoint says "OK, here's a code for you" by redirecting your browser to a "localhost" website which. But this is a mere terminal application, a command-line interface utility (CLI.) So you'll notice that the process of acquiring the access_token and refresh_token employs a somewhat hacky (but working and completely permissible) method to get the OAuth code. OAuth basically assumes there's a web app ready to receive an authorization code.

Have you ever wanted to programmatically list, or download (or even selectively delete or rename) all your files from your OneDrive account via Python?ĭisclaimer: This utility is literally just a few hours of coding, so don't expect it to catch all the edge-cases.Īuthentication: Microsoft, like most companies, uses OAuth to grant you access to your stuff.

Or, maybe you can do one big "select all" operation, but then it tries to create a gigantic Zip file, and maybe your internet connection doesn't like gigantic multi-gigabyte files. You typically have to select each of them, item by item, week by week or folder by folder. At this writing (Jan 2023), the OneDrive user interface makes it hard to download just your photos and videos in one go.
