Package: aiopyo365
aiopyo365Async wrapper for Python >= 3.8 aroundMicrosoft v1.0 graph API.Installationpip install aiopyo365Requirementspython 3.8 or greaterApplication registrationMicrosoft Graph APi requires to be authentificated. You will need to
have aregistred applicationin Azure that will provide you:client idclient secretYou will also need to have therequired permissionsto be able to interact withthe desired ressources.Installation#TODOAuthentificationTo authentificate you can use theGraphAuthProviderclass in theproviders.auth module.here is how to use this class. it assumes that you have set the folowing environnement variables :CLIENT_IDCLIENT_SECRETTENANT_IDThe class provide a method to fetch the token in the
form of adict.importasynciofromaiopyo365.providers.authimportGraphAuthProviderasyncdeffetch_auth_header():auth_provider=GraphAuthProvider(client_id=os.environ["CLIENT_ID"],client_secret=os.environ["CLIENT_SECRET"],tenant_id=os.environ["TENANT_ID"],)returnawaitauth_provider.auth()if__name__=='__main__':auth_header=asyncio.run(fetch_auth_header())print(auth_header)# output : {"authorization": "<token type> <token>"}RessourcesThe library tries to resemble the organization of the graph API documentation.for instance in the Graph documentation you will find theDriveItemsunder theFilessection.Inaiopyo365:fromaiopyo365.ressources.filesimportDriveItemsIf you want to work directly with ressources class you will need to instanciate aaiohttp sessionwithauth headerand instanciate the client class.importasyncioimportaiohttpfromaiopyo365.ressources.filesimportDriveItemsasyncdefupload_smallfile(content,file_name):auth_provider=GraphAuthProvider(client_id=os.environ["CLIENT_ID"],client_secret=os.environ["CLIENT_SECRET"],tenant_id=os.environ["TENANT_ID"],)auth_header=awaitauth_provider.auth()session=awaitaiohttp.ClientSession(headers=auth_header)drive_items_client=DriveItems(base_url="url",session=session)awaitdrive_items_client.upload_small_file(content,file_name)You can also use factories
to work with variant of ressources
here we work with a driveItems dedicated to SharePoint (site).importasyncioimportaiohttpimportosfromaiopyo365.providers.authimportGraphAuthProviderfromaiopyo365.factories.drive_itemsimportDriveItemsSitesFactoryasyncdefupload_smallfile(content,file_name):auth_provider=GraphAuthProvider(client_id=os.environ["CLIENT_ID"],client_secret=os.environ["CLIENT_SECRET"],tenant_id=os.environ["TENANT_ID"],)auth_header=awaitauth_provider.auth()session=awaitaiohttp.ClientSession(headers=auth_header)drive_items_client=DriveItemsSitesFactory(site_id="site_id").create(session=session)awaitdrive_items_client.upload_small_file(content,file_name)Servicesaiopyo365provide also service class that encapsulate many ressource to match business logic. It hides dealing with instanciate class client and so on.
Let's reuse the upload of a file example from above and use theSharePointServiceimportosfromaiopyo365.providers.authimportGraphAuthProviderfromaiopyo365.services.sharepointimportSharePointServiceasyncdefupload_smallfile(content,file_name):auth_provider=GraphAuthProvider(client_id=os.environ["CLIENT_ID"],client_secret=os.environ["CLIENT_SECRET"],tenant_id=os.environ["TENANT_ID"],)asyncwithSharePointService(auth_provider,"SHAREPOINT_HOSTNAME","SHAREPOINT_SITE")assharepoint:resp=awaitsharepoint.upload(small_file_path,"small_file",conflict_behavior="replace")assertresp["createdDateTime"]