Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Office 365 REST API (Python) Mark Email as Read

I'm sure I'm doing something simple wrong, but I can't for the life of me figure out how to set the "IsRead" property to true. It's the last step of my process that gets a filtered list of messagesa and stores and processes any attachments.

According to the docs "IsRead" is writable: http://msdn.microsoft.com/office%5Coffice365%5CAPi/complex-types-for-mail-contacts-calendar#ResourcesMessage

http://msdn.microsoft.com/office%5Coffice365%5CAPi/mail-rest-operations#MessageoperationsUpdatemessages

I'm using python 2.7 and the requests module:

# once file acquired mark the email as read
params = {'IsRead':'True'}
base_email_url = u'https://outlook.office365.com/api/v1.0/me/messages/{0}'.format( msgId )
response = requests.patch(base_email_url, params, auth=(email,pwd))
log.debug( response )

The response that comes back is this:

{"error":{"code":"ErrorInvalidRequest","message":"Cannot read the request body."}}

What's the problem with my request?

like image 929
torpy Avatar asked Oct 20 '22 20:10

torpy


2 Answers

At first glance it looks OK. I wonder if the Content-Type header isn't being set to "application/json" or something along those lines. Try getting a network trace and verify that the request looks something like:

PATCH https://outlook.office365.com/api/v1.0/Me/Messages('msgid') HTTP/1.1
Accept: application/json;odata.metadata=full
Authorization: Bearer <token>
Content-Type: application/json;odata.metadata=full
Host: outlook.office365.com
Content-Length: 24
Expect: 100-continue
Connection: Keep-Alive

{
  "IsRead": "true"
}
like image 112
Jason Johnston Avatar answered Oct 22 '22 22:10

Jason Johnston


Well I have an answer for myself and it is indeed a simple matter. It was a mistake to not fully read how PATCH is different from GET or POST. In short it's important to make sure your headers are set for the right content-type.

Here is the working code:

# once file acquired mark the email as read
changes = {u'IsRead':u'True'}
headers = {'Content-Type': 'application/json'}
json_changes = json.dumps(changes)
base_email_url = u'https://outlook.office365.com/api/v1.0/me/messages/{0}'.format( msgId )
response = requests.patch(base_email_url, data=json_changes, auth=__AUTH, headers=headers)
log.debug( response )
like image 27
torpy Avatar answered Oct 22 '22 23:10

torpy