Failed to send message with Error "CompactToken parsing failed with error code: 80049217"

Oleti, Nagi Reddy 0 Reputation points
2023-02-15T03:53:41.39+00:00

Hi everyone,
My requirement is to send Teams messages to specific users when conditions are satisfied.
I am using the below URL to get the access token :
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
I am able to get the token successfully.
passing it to the header in the below format:

headers = {
        "Authorization": "Bearer" + access_token,
        "Content-Type": "application/json"
    }

URL used to post is:
"url = "https://graph.microsoft.com/v1.0/users/me/sendMail""
below are the API permission added:
User's image

Error Message:

"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217",
Can anyone help me to troubleshoot this issue?

Thanks in advance for your help!

Microsoft Security Microsoft Graph
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-02-15T05:07:20.95+00:00

    Hi Oleti, Nagi Reddy,

    Thanks for reaching out!

    As per your requirement to send messages in teams, you can use below endpoint,

    POST /chats/{chat-id}/messages

    You can refer more on this link for sending messages in teams.

    The Graph API request for sending the mail should be in the below manner:

    POST https://graph.microsoft.com/v1.0/users/ {id | userPrincipalName}/sendMail

    POST https://graph.microsoft.com/v1.0/me/sendMail

    Reference link: Send Mail

    As per the screenshot, you need to consent to the required permissions as per your request.

    For sending mail, consent to "Mail.Send".

    For additional Information, you can refer this link.

    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."

    0 comments No comments

  2. CarlZhao-MSFT 46,366 Reputation points
    2023-02-15T07:53:43.1366667+00:00

    Hi @Oleti, Nagi Reddy

    If your app is sending mail on behalf of a specific user, then you must use the client credentials flow to obtain a token. Since the client credential flow does not support the /common endpoint, you must change /common to /{tenant id}.

    Before requesting a token, grant Mail.Send application permissions to your app and grant admin consent.

    22

    Note that if your app is multi-tenant app and you're trying to send mail on behalf of users from another tenant, then you'll also need to add your multi-tenant app as an enterprise app to the target tenant. This requires you to run the admin consent URL in the browser and sign in and consent with the admin of the target tenant.

    https://login.microsoftonline.com/{tenant id of the target tenant}/adminconsent?client_id={client id}
    

    Request a token:

    User's image

    Send Mail:

    User's image

    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.


  3. Marco Scheel 0 Reputation points
    2023-06-21T08:18:09.4833333+00:00

    We are facing a similar issue. While researching I've found a few posts and the problem was (not ours :() the missing space between the Bearer and the token:

    Minor change but this should work if all requests are failing with your error message.

    headers = {
    "Authorization": "Bearer " + access_token,
    "Content-Type": "application/json"
    }
    
    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.