The Python Oracle

Office 365 REST API (Python) Mark Email as Read

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping

--

Chapters
00:00 Question
00:58 Accepted answer (Score 2)
01:26 Answer 2 (Score 1)
01:58 Thank you

--

Full question
https://stackoverflow.com/questions/2748...

Question links:
http://msdn.microsoft.com/office%5Coffic...
http://msdn.microsoft.com/office%5Coffic...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #rest #python27 #office365

#avk47



ACCEPTED ANSWER

Score 2


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



ANSWER 2

Score 1


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 )