I think one downside of all the emails and automated reporting we do nowadays is that people tend to start using their emails as document storage. I also means that when someone else needs to be brought up to speed on a certain operation either all previous emails need to forwarded or hope that they are saved on a NAS. Even worse sometimes they are stored on personal drives.
One way in which I have been trying to prevent those issues is to commit to keeping up the SharePoint site for my department up to date, which includes copies of all historical automated reports that are sent out to executives. This allows for there to be a central repository of these reports, allows not technical personnel to search for them easily, and the day to day management doesn't need to be handled by me but just someone with a bit of experience (It also allows to use Flow and PowerApps for some stuff, but that's outside the scope of this post). Now SharePoint (an the whole Office 365) is not my favorite but you gotta work with what you have got, and I will admit that their API is pretty easy to use.
Microsoft Graph API
Setup
A quick overview of Microsoft Graph can be found on MS site. Basically its the gateway we can use to access all sorts of information in Office 365.
In order for us to use the API we must first register our application on the Azure Portal. The Microsoft documentation is pretty good at explaining the process. The main information we will need at the end is
Client ID
Client Secret
With just that information we can then generate an Oauth2 Authorization token and start getting data from office 365. For example we could use postman to make all the graph calls.
Postman
Using Postman is pretty straight forward. We just need to set the authorization to use the Oauth2 protocol.
Then we can generate the first token, {tenant} will be specific to your organizations as will your Client ID and Client Secret.
Now we can select our specific token and make calls to microsoft graph form the API.
requests_oauthlib
However I don't want to use postman to update handle my interactions with MS graph. We can easily do this directly from python.
First as the documentation tells us to do we install the package
pip install requests requests_oauthlib
requests_oauthlib makes it easy to manage an Oauth2 Session. We can start a session and make http calls with it by just doing (assuming we are using a config module to keep all our necessary parameters) the following.
Note: I prefer to create a config module that holds all my credentials and then import that, obviously this isn't secure at all so other methods would be used if we are worried about securty
We still need to obtain a token for so well need to authorize our application for the requested scopes. Afterwards we will obtain the authorization code from the response.
The login function generates a URL that asks for authorization from the user (along with the login to Office 365). This generates an authorization code that then will be used to authorize our application and generate a token.
Now that we have our initial token, we also have a refresh_token that can be used to generate new tokens when one expires.
requests_oauthlib already can take care of this. It will just change the initialization or our session object.
The important part of this call is the token_updater parameter which lets us define a function that saves our refreshed token. For example if we were saving it to a text file it would look like this:
SharePoint Class
Since we are going to be using this specific method of uploading and dealing with our SharePoint site quite often we chose to create a class to handle all of that.
Initialization
First we will need to initialize our class. In the initialization has two different paths depending on if redis or a text token storage is used (one of the computers I use to run some code is still on Win 7 so redis on docker won't work).
We will also need to check if the token exists at the location specified or not. If it is not it will kick off the auth workflow previously defined.
In the case that the token needs to be created the authorization process will be to create a oauth session without it request the login and authorization process to get a token and then create a new session using the newly created token and the correct token updater function for that specific token storage type.
Now that we have the session created we can move on to define methods to simplify our interactions with SharePoint.
SharePoint Methods
Since I mostly will use this class to upload files into SharePoint document libraries the methods I define are mostly to navigate the file structure.
The first one we will need is to be able to find to set the specific site we are dealing with and also getting all the drives (document libraries in it)
Once we know all the drives and their id's we can define functions that allow us to set the drive we are working it and also navigate the children that exist.
Now that we have a way to navigate the document library we can go ahead and upload different documents into the library
So if we wanted to upload a pdf file into a certain folder. We can do the following:
This will upload the file called super_cool_report.pdf to the selected document library.
One good thing about using this with Office 365 is that I can add a check to see when any of my reports run and upload a file into the SharePoint without writing any code.
Power Automate (previously Flow) already has a recipe to do so.