Graph API not recognizing user

Norris Mwiinga 20 Reputation points
2023-02-21T13:08:33.3466667+00:00

I have been trying to access my inbox via the Graph API, I have registered an app on the azure portal and the API permissions have been granted. The problem is every time I try to access my inbox via my Laravel project to this endpoint "https://graph.microsoft.com/v1.0/users('******@zynle.com')/mailFolders/inbox/messages" I get an error saying:

{
    "error": {
        "code": "ErrorInvalidUser",
        "message": "The requested user '******@zynle.com' is invalid.",
        "innerError": {
            "date": "2023-02-21T12:51:14",
            "request-id": "620119c6-0988-4f2a-815a-4307cdeec499",
            "client-request-id": "620119c6-0988-4f2a-815a-4307cdeec499"
        }
    }
}

yet on my azure portal, the account "******@zynle.com" is the one I used to register the Application. Below are the API permissions that I have granted :
Screenshot 2023-02-21 150439

I am able to get an access token, The only part I get issues with is reading the outlook inbox.
Please help me I've been at this for a while.

Outlook Windows Classic Outlook for Windows For business
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. HarmeetSingh7172 4,826 Reputation points
    2023-02-21T14:20:35.98+00:00

    Hello Norris Mwiinga,

    Thanks for reaching out!

    The mentioned error message code tells that the user is invalid or doesn't exist.

    1. Make sure the user exists in portal.office.com. I'm able to replicate the error message when I'm passing wrong UPN (non-existing user UPN).
    2. If the user exists, using this endpoint GET https://graph.microsoft.com/v1.0/users, you can query all the users in your tenant. In the response, the response contains id and userPrincipalName that can be used to request the /messages endpoint.
    3. Make sure the token has right permissions/scopes to access the user's mailbox.
    4. Split the application calls into smaller modules and check where it's breaking.

    You can refer to this documentation to get a token.

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Andy David - MVP 157.4K Reputation points MVP Volunteer Moderator
    2023-02-21T13:19:04.82+00:00

    Does this work?

    https://graph.microsoft.com/v1.0/me/messages

    What is the primary SMTP address of the account? Does it match the user logon of ******@zynle.com

    1 person found this answer helpful.

  2. CarlZhao-MSFT 46,366 Reputation points
    2023-02-22T08:34:00.84+00:00

    Hi @Norris Mwiinga

    Try changing the UPN to the user's object id to call that endpoint:

    https://graph.microsoft.com/v1.0/users/{user id}/mailFolders/inbox/messages
    

    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.

    1 person found this answer helpful.

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.