It sounds like some of the required API permissions are missing for the Managed Identity. You can try assigning the following API permissions and try again
Azure Active Directory > Select the application > API Permissions > Add a Permission > Microsoft Graph > Delegated Permissions > Assign the following permissions one by one and try again. I guess, you need only read permissions as indicated in the code.
If it didn't help, try assigning application permissions also (Microsoft Graph > Application Permissions)
----------
--please don't forget to upvote
and Accept as answer
if the reply is helpful--