Share via

MSGraph API 400 Authentication Error with /users/{id}/messages

Brennan Ceballos Peña 0 Reputation points
2023-08-09T10:18:26.6666667+00:00

Hello,

I'm attempting to read the emails of a user who's part of my Azure Active Directory as part of an automation test. I have to preface I'm not using the MSAL library as I want the implementation to be as lightweight as possible.

I've authenticated and gotten a token fine by using ClientCredentials (tenant id, client id and secret) and by using jwt.ms, I've verified that the token has the correct permissions "roles": [ "User.Read.All", "Mail.Read" ]and hasn't expired.

The issue I'm having is I can get a list of the users by doing a get("https://graph.microsoft.com/v1.0/users", headers=self._headers()" However as soon as I try to get the e-mails with get(f"https://graph.microsoft.com/v1.0/users/{user_id}/messages", headers=mail_headers) It will always return a 400 AuthenticationError, Error authenticating with resource.

This is def _headers(self): return {"Authorization": f"Bearer {self.azure_client.token"}", "Host":"graph.microsoft.com"}which just returns the token in a dictionary for the requests rather than typing it out constantly and mail_headers just appends "Prefer":"outlook.body-text"to the headers as per the documentation.

Any advice or things I should check would be greatly appreciated.

Thanks in advance,

Brennan

Microsoft Security | Microsoft Graph
0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-08-11T16:55:11.0966667+00:00

    Hi DP,

    Thanks for reaching out. For Graph SDK related queries please post them on the respective Graph Github SDK forum or the Graph Feedback Forum. Can you reproduce the issue using Graph Explorer or Postman applications?

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

    Thanks.

    Was this answer helpful?

    0 comments No comments

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.