I have a problem sending messages using the Outlook Python API
. I don’t know if it’s possible to send messages using the Outlook Python API
, but i think i found the right library (pyOutlook
), but when trying to send a message an error occurs:
pyOutlook.internal.errors.AuthError: Access Token Error, Received 401 from Outlook REST Endpoint with the message: {'error': {'code': 'InvalidAudienceForResource', 'message': "The audience claim value is invalid for current resource. Audience claim is 'https://graph.microsoft.com/', request url is 'https://outlook.office.com' and resource type is 'Exchange'.", ' innerError': {'oAuthEventOperationId': 'dd2d8b57-200a-47c2-8529-ccf5aff9be86', 'oAuthEventcV': 'D2DvhIH/mNCjZ2l6gxyRjw.1.1.1', 'errorUrl': 'https://aka.ms/autherrors#error -InvalidResource', 'requestId': '84ef600f-ff81-d098-a367-697a831c918f', 'date': '2024-01-15T19:19:01'}}}
As i understand it, the error is that the token is incorrect, but then what am i doing wrong?
Code i’m trying:
from pyOutlook import *
import requests
url="https://login.microsoftonline.com/common/oauth2/v2.0/token"
data = {
'grant_type': 'client_credentials',
'client_id': 'client_id',
'scope': 'https://graph.microsoft.com/.default',
'client_secret': 'client_secret'
}
r = requests.post(url, data=data)
token = r.json().get('access_token')
print(token)
account = OutlookAccount(token)
message = Message(account, 'A body', 'A subject', [Contact('[email protected]')])
message.attach(bytes('some bytes', 'utf-8'), 'bytes.txt')
message.send()