This example shows how to give a Managed Identity Graph API write access to a sigle Sharepoint site using the Graph API Permission "Sites.Selected".
Give the managed identity the 'Sites.Selected' permission using this code.
Use the PnP api to add write permission to the Managed Identity. Using this code.