Access Denied Error When Using App Authentication for Shared Mailbox

Vazquez Romero, Adrian 20 Reputation points
2025-03-13T10:26:25.8033333+00:00

Hello,

I’m developing an application that checks emails in a shared mailbox and moves them to different folders based on certain variables. So far, I’ve managed to do this using delegated access, logging in through a browser with my account. However, since I want to run it on a server, I need it to work without user authentication each time.

Everything works correctly with delegated access, and I can move emails in both my account and the shared mailbox. However, after switching to app authentication, I get the following error:

APIError Code: 403 Message: None Error: MainError(code='ErrorAccessDenied', message='Access is denied. Check credentials and try again.')

The Microsoft Entra admin granted full permissions to this application, which is why it works with delegated access. What could be causing this issue?

My code:

scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credential, scopes=scopes)

query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
            select=['from', 'isRead', 'receivedDateTime', 'subject'],
            top=25,
            orderby=['receivedDateTime DESC']
        )
        
request_config = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
            query_parameters=query_params
        )
        
        # 
messages = await client.users.by_user_id('******@majorel.com').messages.get(
            request_configuration=request_config
        )

Thank you.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,257 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andy David - MVP 153.5K Reputation points MVP
    2025-03-13T11:41:30.69+00:00

    How was access granted exactly?

    They should be following this model:

    https://learn.microsoft.com/en-us/exchange/permissions-exo/application-rbac

    or the older architecture:

    https://learn.microsoft.com/en-us/graph/auth-limit-mailbox-access

    0 comments No comments

  2. CarlZhao-MSFT 45,761 Reputation points
    2025-03-14T02:44:31.9466667+00:00

    Hi @Vazquez Romero, Adrian

    Make sure you have assigned one of the following application permissions to the calling app and granted admin consent for the permissions.

    User's image

    Export your access tokens and decode using jwt.ms and you should be able to see them in the roles claim.

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

Your answer

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