novoid / exchange2org Goto Github PK
View Code? Open in Web Editor NEWConnects to your Exchange server and extracts data in Org-mode format
License: GNU General Public License v3.0
Connects to your Exchange server and extracts data in Org-mode format
License: GNU General Public License v3.0
My company started using OAuth some weeks ago and the script is no longer working.
ERROR UnauthorizedError: Invalid credentials for https://outlook.office365.com/EWS/Exchange.asmx
Retry: 0
Waited: 10
Timeout: 120
Session: 24660
Thread: 139687554021184
Auth type: <requests.auth.HTTPBasicAuth object at 0x7f0b86a30af0>
URL: https://outlook.office365.com/EWS/Exchange.asmx
HTTP adapter: <requests.adapters.HTTPAdapter object at 0x7f0b86a309a0>
Allow redirects: False
Streaming: False
Response time: 0.1890680180003983
Status code: 401
Request headers: {'User-Agent': 'exchangelib/4.4.0 (python-requests/2.26.0)', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Length': '463', 'Authorization': 'Basic anVsb3BlemNAYWNjaW9uYS5jb206QWN0aXZpdHlXYXRjaC0xMjM='}
Response headers: {'Server': 'Microsoft-IIS/10.0', 'request-id': 'c9174f89-17b7-df9b-b006-fff12e1f60b2', 'Alt-Svc': 'h3=":443",h3-29=":443"', 'X-CalculatedFETarget': 'DB8PR09CU001.internal.outlook.com', 'X-BackEndHttpStatus': '401, 401', 'Set-Cookie': 'exchangecookie=756961bcade04553869ddc02c545003a; expires=Wed, 16-Nov-2022 12:50:42 GMT; path=/; secure; HttpOnly', 'X-FEProxyInfo': 'DB8PR09CA0012.EURPRD09.PROD.OUTLOOK.COM', 'X-CalculatedBETarget': 'DB7PR05MB5785.eurprd05.prod.outlook.com', 'X-RUM-Validated': '1', 'X-BeSku': 'WCS5', 'X-DiagInfo': 'DB7PR05MB5785', 'X-BEServer': 'DB7PR05MB5785', 'X-Proxy-RoutingCorrectness': '1', 'X-Proxy-BackendServerStatus': '401', 'X-FEServer': 'DB8PR09CA0012, MR1P264CA0016', 'X-FirstHopCafeEFZ': 'MRS', 'X-Powered-By': 'ASP.NET', 'WWW-Authenticate': 'Basic Realm=""', 'Date': 'Tue, 16 Nov 2021 12:50:42 GMT', 'Content-Length': '0'}
Request XML: b'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><s:Header><t:RequestServerVersion Version="Exchange2019"/></s:Header><s:Body><m:ResolveNames ReturnFullContactData="false"><m:UnresolvedEntry>[email protected]</m:UnresolvedEntry></m:ResolveNames></s:Body></s:Envelope>'
Response XML: b''
CRITICAL Error occured while trying to set up connection with the exchange server "outlook.office365.com":
Traceback (most recent call last):
File "/home/julian/code/exchange2org/exchange2org/__init__.py", line 426, in <module>
main()
File "/home/julian/code/exchange2org/exchange2org/__init__.py", line 413, in main
exchange2org = Exchange2Org(exchange2orgconfig, logging.getLogger())
File "/home/julian/code/exchange2org/exchange2org/__init__.py", line 157, in __init__
self.account = exchangelib.Account(self.config.PRIMARY_SMTP_ADDRESS, config=self.exchange_config, autodiscover=False, access_type=exchangelib.DELEGATE)
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/account.py", line 133, in __init__
self.version = self.protocol.version
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/protocol.py", line 452, in version
self.config.version = Version.guess(self, api_version_hint=self._api_version_hint)
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/version.py", line 229, in guess
list(ResolveNames(protocol=protocol).call(unresolved_entries=[name]))
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/resolve_names.py", line 36, in call
for elem in self._chunked_get_elements(
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/common.py", line 198, in _chunked_get_elements
yield from self._get_elements(payload=payload_func(chunk, **kwargs))
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/common.py", line 216, in _get_elements
yield from self._response_generator(payload=payload)
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/common.py", line 182, in _response_generator
response = self._get_response_xml(payload=payload)
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/common.py", line 296, in _get_response_xml
r = self._get_response(payload=payload, api_version=api_version)
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/services/common.py", line 251, in _get_response
r, session = post_ratelimited(
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/util.py", line 874, in post_ratelimited
protocol.retry_policy.raise_response_errors(r) # Always raises an exception
File "/home/julian/.local/lib/python3.9/site-packages/exchangelib/protocol.py", line 671, in raise_response_errors
raise UnauthorizedError('Invalid credentials for %s' % response.url)
exchangelib.errors.UnauthorizedError: Invalid credentials for https://outlook.office365.com/EWS/Exchange.asmx
It seems one has to register the application in Azure:
http://fendertech.blogspot.com/2021/03/using-exchangelib-to-test-ews-oauth2.html
Here's the docs on OAuth:
https://ecederstrand.github.io/exchangelib/#oauth-authentication
I'll get on this when I have the time, but I'm not sure how to proceed with it though.
Do I register the app with my account? Do you want to set it up yourself @novoid?
Thank you very much for writing this piece of code. I was very keen on trying this since I have a similar need.
However, after a fresh pip install exchangelib
I get the following error when running the code
# config file is provided and set up correctly to the best of my knowledge
$ python exchange2org/__init__.py --calendar ~/org/calendar.org
CRITICAL Error occured while trying to set up connection with the exchange server "......":
Traceback (most recent call last):
File ".../exchange2org/exchange2org/__init__.py", line 426, in <module>
main()
File ".../exchange2org/exchange2org/__init__.py", line 413, in main
exchange2org = Exchange2Org(exchange2orgconfig, logging.getLogger())
File ".../exchange2org/exchange2org/__init__.py", line 158, in __init__
self.tz = exchangelib.EWSTimeZone.timezone(self.config.TIMEZONE)
AttributeError: type object 'EWSTimeZone' has no attribute 'timezone
Possibly sensitive paths and URLs are redacted in the above, I hope you understand. But I don't think they have much bearing on the issue at hand.
According to the exchangelib changelog the EWSTimeZone.timezone()
method has been deprecated and was removed in version 4.7.0
.
I hoped installing version 4.6.2
would fix this, which it did, but now I get
.../exchange2org/exchange2org/__init__.py:158: DeprecationWarning: replace EWSTimeZone.timezone() with just EWSTimeZone()
self.tz = exchangelib.EWSTimeZone.timezone(self.config.TIMEZONE)
.../exchange2org/exchange2org/__init__.py:314: DeprecationWarning: replace tz.localize() with dt.replace(tzinfo=tz)
events = self.account.calendar.view(start=self.tz.localize(exchangelib.EWSDateTime(*startday)), end=self.tz.localize(exchangelib.EWSDateTime(*endday)))
Traceback (most recent call last):
File ".../exchange2org/exchange2org/__init__.py", line 426, in <module>
main()
File ".../exchange2org/exchange2org/__init__.py", line 414, in main
exchange2org.dump_calendar(startday=startday, endday=endday)
File ".../exchange2org/exchange2org/__init__.py", line 333, in dump_calendar
output = self.convert_to_orgmode(event)
File ".../exchange2org/exchange2org/__init__.py", line 205, in convert_to_orgmode
start_day = event.start.astimezone(self.tz).ewsformat()[:10]
AttributeError: 'EWSDate' object has no attribute 'astimezone'
So with which last version of exchangelib
this script worked? As a short-term solution?
Do you have plans to bring update the code to the current exchangelib
API? As a longer-term solution.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.