O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.69k stars 424 forks source link

Mailbox 404 Error #170

Closed jwhugheskp closed 5 years ago

jwhugheskp commented 5 years ago

This might have more to do with the shared mailbox permissions but if so I can't figure out what to ask the mailbox team. I'm hoping someone can provide a direction.

I've got code that reads new email from 3 different mailboxes. One is my work email. The other two are shared mailboxes. When I run the code I get this error

[splunk@cnpdcspsh106 ~]$ /apps/splunk/bin/splunk cmd /apps/splunk/etc/apps/kp_outlook_inputs/bin/get_mailboxes_email.sh > output.txt
Client Error: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/spam@kp.org/mailFolders/Inbox/messages?%24top=50&%24filter=createdDateTime+gt+2019-01-01T00%3A00%3A00%2B00%3A00 | Error Message: The specified object was not found in the store., The process failed to get the correct properties.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/O365/connection.py", line 542, in _internal_request
    response.raise_for_status()  # raise 4XX and 5XX error codes.
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/spam@kp.org/mailFolders/Inbox/messages?%24top=50&%24filter=createdDateTime+gt+2019-01-01T00%3A00%3A00%2B00%3A00

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/apps/splunk/etc/apps/kp_outlook_inputs/bin/get_mailboxes_email.py", line 146, in <module>
    main()
  File "/apps/splunk/etc/apps/kp_outlook_inputs/bin/get_mailboxes_email.py", line 138, in main
    print_email(mailbox)
  File "/apps/splunk/etc/apps/kp_outlook_inputs/bin/get_mailboxes_email.py", line 83, in print_email
    for message in inbox.get_messages(limit=50, query=new_email):
  File "/usr/local/lib/python3.7/site-packages/O365/mailbox.py", line 190, in get_messages
    response = self.con.get(url, params=params)
  File "/usr/local/lib/python3.7/site-packages/O365/connection.py", line 628, in get
    return self.oauth_request(url, 'get', params=params, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/O365/connection.py", line 617, in oauth_request
    return self._internal_request(self.session, url, method, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/O365/connection.py", line 582, in _internal_request
    raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/spam@kp.org/mailFolders/Inbox/messages?%24top=50&%24filter=createdDateTime+gt+2019-01-01T00%3A00%3A00%2B00%3A00 | Error Message: The specified object was not found in the store., The process failed to get the correct properties.

[splunk@cnpdcspsh106 ~]$ grep -A1 Requesting output.txt
Requesting (GET) URL: https://graph.microsoft.com/v1.0/users/joseph.hughes@kp.org/mailFolders/Inbox/messages
Request parameters: {'params': {'$top': 50, '$filter': 'createdDateTime gt 2019-01-15T21:01:30+00:00'}, 'allow_redirects': True}
--
Requesting (GET) URL: https://graph.microsoft.com/v1.0/users/phishing@kp.org/mailFolders/Inbox/messages
Request parameters: {'params': {'$top': 50, '$filter': 'createdDateTime gt 2019-01-15T20:41:13+00:00'}, 'allow_redirects': True}
--
Requesting (GET) URL: https://graph.microsoft.com/v1.0/users/spam@kp.org/mailFolders/Inbox/messages
Request parameters: {'params': {'$top': 50, '$filter': 'createdDateTime gt 2019-01-01T00:00:00+00:00'}, 'allow_redirects': True}

What happens is I get email for my account and from the phishing account, but the code then errors on the spam mailbox. I think the issue is some difference between the phishing and spam accounts, and not with the 0365 code. I am listed as owner on both phishing and spam. I can't figure out what permission would be missing that could cause this. I'm looking for ideas.

TIA, Joe

alejcas commented 5 years ago

The error you are getting is Not Found. This means the resource can not be found. Try the same queries under the graph explorer. It it returns the same error it is unrelated to this library.

Also, I've tried a query on one of the shared mailboxes of my company and on my personal inbox and it works as expected.

INFO:O365.connection:Requesting (GET) URL: https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages
INFO:O365.connection:Request parameters: {'params': {'$top': 50, '$filter': 'createdDateTime gt 2018-12-31T23:00:00+00:00'}, 'allow_redirects': True}
...
INFO:O365.connection:Received response (200) from URL https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?%24top=50&%24filter=createdDateTime+gt+2018-12-31T23%3A00%3A00%2B00%3A00

INFO:O365.connection:Requesting (GET) URL: https://graph.microsoft.com/v1.0/users/shared@mycompany.com/mailFolders/Inbox/messages
INFO:O365.connection:Request parameters: {'params': {'$top': 50, '$filter': 'createdDateTime gt 2018-12-31T23:00:00+00:00'}, 'allow_redirects': True}
...
INFO:O365.connection:Received response (200) from URL https://graph.microsoft.com/v1.0/users/shared@mycompany.com/mailFolders/Inbox/messages?%24top=50&%24filter=createdDateTime+gt+2018-12-31T23%3A00%3A00%2B00%3A00

This is what I've done:

import datetime as dt

# ... creating account instance, etc.

my_mailbox = account.mailbox()
shared_mailbox = account.mailbox('shared@mycompany.com')

my_inbox = my_mailbox.inbox_folder()
shared_inbox = shared_mailbox.inbox_folder()

q = my_inbox.q('created_date_time').greater(dt.datetime(2019, 1, 1))

print(my_inbox.get_messages(limit=50, query=q))  # returns 200 ok
print(shared_inbox.get_messages(limit=50, query=q))  # returns 200 ok
alejcas commented 5 years ago

Also, be sure to have the latest version installed. Run pip install --upgrade O365

jwhugheskp commented 5 years ago

Thanks @janscas. I tried your suggestion and it worked for my mailbox and phishing@kp.org. It still doesn't like spam@kp.org. I have to talk to the mail team and see if we can figure out the differences. Also thanks for the website. I've never seen that one.

Regards, Joe