大家好,我是 Microsoft Azure 平台的新手。 我们正在使用 mircosoft outlook graph api 发送邮件。 凭据和一切正常。但是当我们将 api 与 /sendMail 方法一起使用时,它会返回。
访问被拒绝。检查凭据并重试。
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())```