Upload Files To One Drive Using Microsoft Graph API In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how soon guys in this video i'm going to show you how to upload files to onedrive using python all right so here let me go into my project folder and here this is a one png file that i want to upload to my onedrive folder and for this video i'm going to show you two examples so i'm going to show you how to upload a file to the home directory which is the this main page and secondly i'll show you how to upload file to a folder based on the distributed folder id to a folder based on a folder id then i'll show another example how to upload a file to a folder based on the folder path all right so let's start with the first example all right so before we dive into the tutorial make sure that you download this ms grab module from the link in the description below and this is a utility module i row to simplify the access token creation in my script i'm going to import deos module and increase the font size and to make the request code i'm going to import the crisis library and from ms graph i'm going to import the generate access token function as well as the graph api endpoint viewable and to generate an access token i need to provide my app id and here let me just copy paste the id over and for the permission scopes i'm actually going to use this one scope and it's going to be files dot re right scope now i'm going to generate the access token by using the generate access token function and i'll provide the app id and the scopes and once we generate the access token we're going to pass the token to the header software inside the headers object we're going to supply the authorization key and for the value is going to be bill space and the access token right so before we can upload a file to our onedrive account we need to convert the file into uh by strings and we can do that by using the words open context manager and here we're going to insert the file path let me create a vehicle called file path and it's going to be the file location right so here are we open then i'm going to insert the file path and ontario's binary and i'll name the context manager upload then i'm going to reference the upload option diary and i'll store the buy stream into an objective media content all right so once we hit the the buy string of the file we can now make a crystal to upload the file to our onedrive folder and we put this on the side so to upload a file to onedrive we need to use the pull method so from request is that put we're going to supply the api endpoint and it's going to be graph api endpoint followed by the query path and it's going to be slash me slash drive items let me explain the window and to reference the home page of the home directory we're going to insert root followed by a calling the insert four slash and it's going to be the file name actually uh let me create another variable called file name consume the uh should let me change this to the actual file path and give me a second now going back to the script here i'm going to replace file path with file name basically uh what's in there when we upload the file this is what the file name is going to be and if you want you have the option to uh change the file name at this point but i'm going to keep the file name the same then we're going to insert colon adm slash content then we need to provide the headers and to upload the buy stream we're going to assign data with media content option and i'll name the output as response [Applause] and i'll print the response option dot json all right so if i go ahead and run this script now the first thing i need to do here is i need to authenticate my account and once i authenticated my account it's going to make this pull request and upload the file and once the file is uploaded it's going to return the metadata associated to the file let me go ahead and refresh the page and here's the one.png file that i just uploaded using python now if i want to upload a file to a folder and i think this is the most commonly used case so as i want to upload my one.png file to my tutorials folder so first we need to grab the folder id on the top and it's going to be just an id property and here i'm going to create a variable called onedrive folder id and i'll copy this code block so essentially the request is going to be the same except that the endpoint is going to be a little bit different so here we need to replace root with the folder id in colon slash followed by the file name all right so let me go ahead and run the script and if i refresh my page and here's the file all right so let me delete the file first now to upload a file based on the folder path here i'm going to replace the folder id we know the home directory or the pn directory is going to be root colon then i'm going to insert four slash followed by the folder name down to upload the files to and the folder name is going to be tutorials in my file represents roots colon and everything else is going to be the same now if i go ahead and run the script again and plus fi too fast page and here's the file 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 thank you guys for watching i'll see you guys in the next video
Info
Channel: Jie Jenn
Views: 39,063
Rating: undefined out of 5
Keywords: Microsoft Graph API, OneDrive API, Automate OneDrive, Upload File To OneDrive API, OneDrive Python
Id: Ok8O_QnrSBI
Channel Id: undefined
Length: 9min 11sec (551 seconds)
Published: Mon Apr 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.