Created
August 22, 2014 23:11
-
-
Save catermelon/1887d4c0b8eeae69f001 to your computer and use it in GitHub Desktop.
Search for calendar events in exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> | |
<s:Header> | |
<h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" MajorVersion="14" MinorVersion="3" MajorBuildNumber="195" MinorBuildNumber="1"/> | |
</s:Header> | |
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | |
<m:FindItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> | |
<m:ResponseMessages> | |
<m:FindItemResponseMessage ResponseClass="Success"> | |
<m:ResponseCode>NoError</m:ResponseCode> | |
<m:RootFolder TotalItemsInView="20" IncludesLastItemInRange="true"> | |
<t:Items> | |
<t:CalendarItem> | |
<t:ItemId Id="AAAVAHJzYW5kZXJzQGxpbmtlZGluLmNvbQBGAAAAAAD9SMdJZdUJQZD6AaiVjB7QBwC/1jQfGZYxS4scYPVIHHcVAAAAIehGAAD4xglk0Z8+Q7DxECC4c+S/AAAAIj3xAAA=" ChangeKey="DwAAABYAAAD4xglk0Z8+Q7DxECC4c+S/AAADlGDz"/> | |
<t:Subject>Flopping around in a lagoon</t:Subject> | |
<t:HasAttachments>false</t:HasAttachments> | |
<t:Start>2013-09-09T17:00:00Z</t:Start> | |
<t:End>2013-09-09T18:00:00Z</t:End> | |
<t:LegacyFreeBusyStatus>Busy</t:LegacyFreeBusyStatus> | |
<t:Location>my desk</t:Location> | |
<t:CalendarItemType>Single</t:CalendarItemType> | |
<t:Organizer> | |
<t:Mailbox> | |
<t:Name>Hugh Manatee</t:Name> | |
</t:Mailbox> | |
</t:Organizer> | |
</t:CalendarItem> | |
<t:CalendarItem> | |
<t:ItemId Id="AAAVAHJzYW5kZXJzQGxpbmtlZGluLmNvbQFRAAgI0HsGqVJAAEYAAAAA/UjHSWXVCUGQ+gGolYwe0AcAv9Y0HxmWMUuLHGD1SBx3FQAAACHoRgAAv9Y0HxmWMUuLHGD1SBx3FQAAT9mX/AAAEA==" ChangeKey="DwAAABYAAAD4xglk0Z8+Q7DxECC4c+S/AAADlHnK"/> | |
<t:Subject>Stomping around and eating things</t:Subject> | |
<t:HasAttachments>false</t:HasAttachments> | |
<t:Start>2013-09-09T20:45:00Z</t:Start> | |
<t:End>2013-09-09T21:00:00Z</t:End> | |
<t:LegacyFreeBusyStatus>Tentative</t:LegacyFreeBusyStatus> | |
<t:Location/> | |
<t:CalendarItemType>Occurrence</t:CalendarItemType> | |
<t:Organizer> | |
<t:Mailbox> | |
<t:Name>T Rex</t:Name> | |
</t:Mailbox> | |
</t:Organizer> | |
</t:CalendarItem> | |
</t:Items> | |
</m:RootFolder> | |
</m:FindItemResponseMessage> | |
</m:ResponseMessages> | |
</m:FindItemResponse> | |
</s:Body> | |
</s:Envelope> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2 | |
from ntlm import HTTPNtlmAuthHandler | |
from httplib import HTTPException | |
from lxml import etree | |
EXCHANGE_URL = 'https://blahblah.org/EWS/Exchange.asmx' | |
EXCHANGE_USER = 'DOMAIN\\username' | |
EXCHANGE_PASSWORD = "password" | |
class EventMaker(object): | |
""" | |
Tool to create calendar events in MS Exchange | |
""" | |
def __init__(self, url, user, password): | |
self.url = url | |
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
passman.add_password(None, url, user, password) | |
# create the NTLM authentication handler | |
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman) | |
# create and install the opener | |
self.opener = urllib2.build_opener(auth_NTLM) | |
def build_soap_xml_request(self): | |
xml_decl = '<?xml version="1.0" encoding="utf-8"?>' | |
FIND_CALENDAR_EVENTS = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" | |
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> | |
<soap:Body> | |
<m:FindItem Traversal="Shallow"> | |
<m:ItemShape> | |
<t:BaseShape>Default</t:BaseShape> | |
</m:ItemShape> | |
<m:CalendarView MaxEntriesReturned="100" StartDate="2013-09-09T12:00:00Z" EndDate="2013-09-13T12:00:00Z" /> | |
<m:ParentFolderIds> | |
<t:DistinguishedFolderId Id="calendar"/> | |
</m:ParentFolderIds> | |
</m:FindItem> | |
</soap:Body> | |
</soap:Envelope>""" | |
soap_request = FIND_CALENDAR_EVENTS | |
# change here whatever you want to send. | |
return "".join([xml_decl,soap_request]) | |
def make_soap_request(self, request_xml, retries=2): | |
req = urllib2.Request(self.url, request_xml) | |
req.add_header("Accept", "text/xml") | |
req.add_header("Content-type", "text/xml; charset=utf-8 ") | |
for retry in xrange(retries + 1): | |
try: | |
# retrieve the result | |
response = self.opener.open(req) | |
return response | |
except HTTPException as err: | |
error = err | |
# All retries used up, re-throw the exception. | |
raise error | |
def fetch_raw_xml_response(self, retries=2): | |
req_xml = self.build_soap_xml_request() | |
print req_xml | |
return self.make_soap_request(req_xml, retries) | |
if __name__ == '__main__': | |
if EXCHANGE_PASSWORD is None or EXCHANGE_PASSWORD is "": | |
password = raw_input('password?') | |
else: | |
password = EXCHANGE_PASSWORD | |
e = EventMaker(url=EXCHANGE_URL, user=EXCHANGE_USER, password=password ) | |
response = e.fetch_raw_xml_response() | |
print u'code', response.getcode() | |
print u'info', response.info() | |
print u' ---- body ------' | |
try: | |
body = etree.fromstring(response.read()) | |
print etree.tostring(body, pretty_print=True) | |
except etree.XMLSyntaxError: | |
print "--- [NO RESPONSE] --- " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thought you might like to see what I did
!/usr/bin/env python
import urllib2
from ntlm import HTTPNtlmAuthHandler
from httplib import HTTPException
from lxml import etree
from datetime import date
import datetime
import xml.etree.ElementTree as et
from xml.dom.minidom import parse, parseString
EXCHANGE_URL = 'URL'
EXCHANGE_USER = 'DOMAIN//EMAIL'
EXCHANGE_PASSWORD = "PASSWORD"
tod = date.today()
cur = str(tod)
tom = str(tod + datetime.timedelta(days=1))
class EventMaker(object):
"""
Tool to create calendar events in MS Exchange
"""
if name == 'main':