【问题标题】:Grab any users data from the MS Exchange server从 MS Exchange 服务器获取任何用户数据
【发布时间】:2014-02-26 03:10:20
【问题描述】:

我想从 MS Exchange (Office 365) 接收有关所有用户的信息。我最喜欢的语言是python。我想知道是否有办法使用 python 或其他语言从 MS Exchange 服务器获取任何用户数据(姓名、电子邮件、电话等)。

我找到了这个https://fedorahosted.org/suds/,但它太慢了,而且没有维护。

谢谢

【问题讨论】:

    标签: exchange-server exchangewebservices


    【解决方案1】:

    好的。我解决了这个问题!

    import urllib2
    import logging
    from xml.etree import ElementTree
    
    logging.basicConfig(level=logging.DEBUG)
    
    MS_EXCHANGE_USERNAME = 'your@email.com'
    MS_EXCHANGE_PASSWORD = 'passwprd'
    MS_EXCHANGE_ALL_USERS_FOLDER_ID = 'bc632172-21fd-1d4a-ace3-m35222131a94'
    MS_EXCHANGE_URL = 'https://outlook.office365.com/ews/Exchange.asmx'
    
    SOAP = """<?xml version="1.0" encoding="UTF-8"?>
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
            <soap:Header>
                <t:RequestServerVersion Version="Exchange2013" />
            </soap:Header>
            <soap:Body>
                <m:FindPeople>
                    <m:IndexedPageItemView BasePoint="Beginning" MaxEntriesReturned="1000" Offset="0"/>
                    <m:ParentFolderId>
                        <AddressListId Id="%s" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />
                    </m:ParentFolderId>
                </m:FindPeople>
            </soap:Body>
        </soap:Envelope>
    """ % MS_EXCHANGE_ALL_USERS_FOLDER_ID
    
    
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(realm='', uri=MS_EXCHANGE_URL, user=MS_EXCHANGE_USERNAME, passwd=MS_EXCHANGE_PASSWORD)
    opener = urllib2.build_opener(auth_handler, urllib2.HTTPHandler(debuglevel=3))
    urllib2.install_opener(opener)
    
    
    req = urllib2.Request(url=MS_EXCHANGE_URL, data=SOAP)
    req.add_header('Content-Type', 'text/xml; charset=utf-8')
    response = urllib2.urlopen(req)
    
    root = ElementTree.fromstring(response.read())
    
    persons = []
    
    namespaces = {'env': 'http://schemas.xmlsoap.org/soap/envelope/',
                  'msg': 'http://schemas.microsoft.com/exchange/services/2006/messages'}
    
    for person in root.find('env:Body/msg:FindPeopleResponse/msg:People', namespaces=namespaces):
        person_data = {
            'first_name': '',
            'last_name': '',
            'email': '',
            'company': '',
            'im': '',
            'created_at': '',
        }
    
        for param in person:
    
            if 'EmailAddresses' in param.tag:
                for email in param[0]:
                    if 'EmailAddress' in email.tag:
                        person_data['email'] = email.text
    
            elif 'GivenName' in param.tag:
                person_data['first_name'] = param.text
    
            elif 'Surname' in param.tag:
                person_data['last_name'] = param.text
    
            elif 'Company' in param.tag:
                person_data['company'] = param.text
    
            elif 'ImAddress' in param.tag:
                person_data['im'] = param.text
    
            elif 'CreationTime' in param.tag:
                person_data['created_at'] = param.text
    
        persons.append(person_data)
    
    
    for i, p in enumerate(persons):
        print str(i).ljust(3), p['first_name'].ljust(15), p['last_name'].ljust(15), p['email'].ljust(35), \
            p['im'].ljust(45), p['company'].ljust(20), p['created_at'].ljust(20)
    
    # TODO: use persons array
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多