Skip to content

Instantly share code, notes, and snippets.

@sandromello
Created May 7, 2014 14:57
Show Gist options
  • Save sandromello/1b46c1518f8c23933ff1 to your computer and use it in GitHub Desktop.
Save sandromello/1b46c1518f8c23933ff1 to your computer and use it in GitHub Desktop.
Change Password Example using Soap Zimbra API
import requests, sys
# XML para obter o token para realizar as chamadas na API
get_token = '<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header><context xmlns="urn:zimbra"><format type="xml"/></context></soap:Header><soap:Body><AuthRequest xmlns="urn:zimbraAdmin"><account by="name">admin@domain.tld</account><password>adminpassword</password></AuthRequest></soap:Body></soap:Envelope>'
# Header da requisicao
req_headers = { 'Content-Type': 'application/soap+xml' }
server = 'https://yourserver:7071/service/admin/soap'
# server: endereco da requisicao; data: dados do corp da requisicao "body content"; header: os headers da requisicao; verify: desabilita checagem de certificados, https urls
r = requests.post(server, data=get_token, headers=req_headers, verify=False)
try:
#Conteudo esperado:
#<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header><context xmlns="urn:zimbra"/></soap:Header><soap:Body><AuthResponse xmlns="urn:zimbraAdmin"><authToken>0_3d8e53caf7bf4bcd5db3211172092121f902e034_69643d33363a61336534366136362d373665352d343639342d626336342d6438353233316462356663363b6578703d31333a313339393531373237333635313b646c6761646d696e3d313a313b76763d313a303b747970653d363a7a676d6272613b</authToken><lifetime>43200000</lifetime></AuthResponse></soap:Body></soap:Envelope>
token = r.content.split('authToken>')[1].split('<')[0]
except Exception:
print r.content
sys.exit(1)
change_password_xml = '<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header><context xmlns="urn:zimbra"><format type="xml"/><authToken>%s</authToken></context></soap:Header><soap:Body><ChangePasswordRequest xmlns="urn:zimbraAccount"><account by="name">myaccount@domain.tld</account><oldPassword>myaccount_old_password</oldPassword><password>myaccount_new_password</password></ChangePasswordRequest></soap:Body></soap:Envelope>' % token
r = requests.post(server, data=change_password_xml, headers=req_headers, verify=False)
# Conteudo esperado ( senha alterada com sucesso ):
#<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header><context xmlns="urn:zimbra"/></soap:Header><soap:Body><AuthResponse xmlns="urn:zimbraAdmin"><authToken>0_bb07cf0d0cdb6472887955b7d09351dcb8c82475_69643d33363a61336534366136362d373665352d343639342d626336342d6438353233316462356663363b6578703d31333a313339393531363530363830333b646c6761646d696e3d313a313b76763d313a303b747970653d363a7a696d6272615b</authToken><lifetime>43200000</lifetime></AuthResponse></soap:Body></soap:Envelope>
print r.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment