Download Files From OneDrive Using Microsoft Graph API In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to show you how to download files in onedrive using microsoft graph api in python all right so here in my onedrive folder i'm going to use this powerpoint file as demonstration and here let me open my python script right so the first thing i want to do here is i want to grab the files id now i'm going to do everything a little bit more manually just because i want to show you the process from the url i want to grab this an ies id and that's going to be the files id i'm going to create a variable to store the id right so me do this i'm going to import the libraries first i'm going to import the os module and to make an api code i'm going to import the requests library and from ms graph module i'm going to import the generate access token function as well as the graph api endpoint variable now to grab my app id and the scopes that i'll be using uh for this exercise all right so for the scopes i'm going to use files.rescue i'm going to create another variable called save location and it's going to be the folder where i'm going to save the files all right so i want to uh change file id to file ids and i'll make this servers and just because we can download multiple files i want to generate my access token and i'm going to insert the generate access token function and i'll insert the app id and the scopes they'll create my headers and it's going to be authorization and for the value it's going to be bill space pass the access token oh so one more thing you can download the ms graph module from the link in the description below and this is a utility module that i wrote to simplify the access token creation all right so the first thing i want to do here is i want to grab the file name get the file name and i'll name the output response file info uh is that right actually let's do this i'm going to array the file ids list i'm going to say for file in file ids actually it's going to be file id and you want to make a request code to get the files information so from requests.get i want to insert the graph api endpoint plus the query path it's going to be me slash drive slash items slash then i'm going to insert the file id then we're going to assign the headers and i don't want to return all the fields i just want to return the file name so from the primes field i'm going to specify the fields i want to return using the select parameter and the film name is going to be named now here i want to grab just the file name file response file info archer dot get actually this should be json.get and i want to get the file name now it's going to be step two downloading onedrive actually one drive file again it's going to another get request so from requesters.get i'm going to insert graph api endpoint and for the query file path it's going to be me slash drive slash items followed by the file id slash content then i'm going to assign the headers and i'll name the output response file content and once this statement is executed the response file content is going to contain the by strings diameter of the file then we can save the file to our local drive so i'm going to combine the save location in the file name and it's going to be writer's binary and i'll name the context manager underscore f then i'll create a file and this will be response file content dot content all right so that's going to be the entire script and it's only uh 28 lines of code which is not that many all right so if i run the script and here i might have a typo yup right here oh so here i forgot the comma right so if i run the script and i need to authenticate my account all right so if i go back to my script and oh so we have a typo this should be a file name now let me try again because my token is already saved as a json file when i authenticate in my account so i can rerun the script and here the script finished in 4.2 seconds and here's my powerpoint file let me open the file for you and here it is all right so this is going to be everything i'm going to share in this video and hopefully you guys found this video useful and as always so you guys watching i'll see you guys in the next video
Info
Channel: Jie Jenn
Views: 28,221
Rating: undefined out of 5
Keywords: Microsoft Graph API, OneDrive API, Automate OneDrive, OneDrive Python, OneDrive Download File
Id: w6YeOkikVFI
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Mon Apr 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.