[Python] Microsoft Graph API's "Access is denied. Check credentials and try again"

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo


    Hello everyone i'm new on Microsoft Azure platform. We are using mircosoft outlook graph api for send mails. The credentials and things are working fine. But when we are using api with /sendMail method it returns.
    ​

    Access is denied. Check credentials and try again.

    authority = f"https://login.microsoftonline.com/{tenant_id}"
    app = msal.ConfidentialClientApplication(
    client_id=CLIENT_ID,
    client_credential=CLIENT_SECRET,
    authority=authority)

    scopes = ["https://graph.microsoft.com/.default"]
    result = None
    result = app.acquire_token_silent(scopes, account=None)

    if not result:
    result = app.acquire_token_for_client(scopes=scopes)

    if "access_token" in result:
    print("Access token created.",result["access_token"])

    if "access_token" in result:
    endpoint = f'https://graph.microsoft.com/v1.0/users/{userId}/sendMail'
    toUserEmail = user_email
    email_msg = {'Message': {'Subject': "Test Sending Email from Python",
    'Body': {'ContentType': 'Text', 'Content': "This is a test email."},
    'ToRecipients': [{'EmailAddress': {'Address': toUserEmail}}]
    },
    'SaveToSentItems': 'true'}

    r = requests.post(endpoint,headers={'Authorization': 'Bearer ' + result['access_token']},json=email_msg)
    if r.ok:
    print('Sent email successfully')
    else:
    print(r.json())```

    Continue reading...

Compartilhe esta Página