How to request email silently in a loop?

Hen Golden 20 Reputation points
2025-10-24T14:30:32.8066667+00:00

Sridevi Machavarapu , I am currently running the following loop to continuously fetch the incoming mail list:

    while True:
        result = await graph_client.me.messages.get(request_configuration=request_configuration)
    
        messages_list = []
        if hasattr(result, "value"):
            for msg in result.value:
                msg_dict = {
                    "id": msg.id,
                    "subject": msg.subject,
                    "from": msg.from_.email_address.address if msg.from_ else None,
                    "bodyPreview": msg.body_preview,
                    "receivedDateTime": str(msg.received_date_time)
                }
                messages_list.append(msg_dict)
        
        print(json.dumps(messages_list, indent=4))
        await asyncio.sleep(1)


Although the app only ask for authentication in a pop-up window for the first iteration, I wonder if the authentication will expire and need to be manually re-authenticate? and in that case, how could I implement this such that it can proceed silently, without the need to intervene?

Microsoft Security | Microsoft Entra | Microsoft Entra ID

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.