Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's some working Python code, but as you noted, the trick is getting the right PEM files:</p> <pre><code>import httplib conn = httplib.HTTPSConnection('management.core.windows.net', cert_file='cert.pem', key_file='key.pem') conn.request('GET', '/%s/services/storageservices' % subscription_id, headers={'x-ms-version': '2011-02-25'}) print conn.getresponse().read() </code></pre> <p>I got my PEM files from <a href="https://github.com/smarx/waz-cmd" rel="nofollow">https://github.com/smarx/waz-cmd</a>, a Ruby-based command-line tool I wrote to interact with the Service Management API. Note that you need both pieces (the <code>cert_file</code> and the <code>key_file</code>).</p> <p>You have the cert file already, though I think you may actually need to convert it via something like <code>openssl</code> to PEM format. For the key file, I think you'll need to export the certificate you created on your Windows machine and then use <code>openssl</code> to export the private key as a PEM file.</p> <p>These days, instead of creating your own certificate, you can download a <code>.publishsettings</code> file from the Windows Azure portal, which generates and gives you a certificate that's ready to go. See <a href="http://blog.smarx.com/posts/calling-the-windows-azure-service-management-api-with-the-new-publishsettings-file" rel="nofollow">http://blog.smarx.com/posts/calling-the-windows-azure-service-management-api-with-the-new-publishsettings-file</a>. The certificate in there is in PFX format, but the right <code>openssl</code> magic should give you the two pieces you need. I actually solved this problem once (for use with <code>curl</code> on a Mac), but I no longer have the exact commands handy. :-( I <em>may</em> give it a whirl and write a blog post about it this evening.</p> <p><strong>EDIT</strong>:</p> <p>Here's a complete Python program that takes a <code>.publishsettings</code> file on the command-line, dumps the key and certificate into a single file (cert.pem) in the current directory, and then uses it to call the service management API and print the names of all your storage accounts. Before running, just <code>pip install lxml pyopenssl</code>.</p> <pre><code>import httplib import argparse from StringIO import StringIO from lxml import etree import base64 from OpenSSL.crypto import * parser = argparse.ArgumentParser() parser.add_argument('file', metavar='file', type=str, help='Your .publishsettings file.') args = parser.parse_args() tree = etree.parse(args.file) pp = tree.find('PublishProfile') cert = load_pkcs12(base64.decodestring(pp.get('ManagementCertificate'))) with open('cert.pem', 'w') as f: f.write(dump_certificate(FILETYPE_PEM, cert.get_certificate())) f.write(dump_privatekey(FILETYPE_PEM, cert.get_privatekey())) subscription_id = pp.find('Subscription').get('Id') conn = httplib.HTTPSConnection('management.core.windows.net', cert_file='cert.pem') conn.request('GET', '/%s/services/storageservices' % subscription_id, headers={'x-ms-version': '2011-02-25'}) for e in etree.parse(StringIO(conn.getresponse().read())).iterfind('//{http://schemas.microsoft.com/windowsazure}ServiceName'): print e.text </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload