How do I access Windows Azure account token using Python 3.3
Question: I am new to programming and I am looking to access Windows translate token
using Python 3.3. MS have guidelines [here](http://msdn.microsoft.com/en-
us/library/ff512433.aspx) for C# and PHP but I don't seem to be able to
replicate their success in Python. The code is falling over at the
HTTPConnection stage.

**Edit [14 May]:** I found some improvements that seem to be helping

My simple code to connect is:

    
    
    from suds.client import Client
    import http.client
    
    # Initialize variables:
    wsdlUrl = "http://api.microsofttranslator.com/V2/Soap.svc"
    clientID = "ID";
    clientSecret = "SECRET"
    authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"
    scopeUrl = "http://api.microsofttranslator.com"
    grantType = "client_credentials"
    
    def getTokens(grantType, scopeUrl, clientID, clientSecret, authUrl):
        headers = {"grant_type": grantType, "client_id": clientID, "client_secret":         clientSecret, "scope": scopeUrl}
        conn = http.client.HTTPSConnection('datamarket.accesscontrol.windows.net')
        conn.request("POST", "/v2/OAuth2-13", "", headers)
        response = conn.getresponse()
        print(response.status, response.reason)
    
    
    getTokens(grantType, scopeUrl, clientID, clientSecret, authUrl)
    

The error now receive is: 400 Bad Request

I have researched this error and where answers were provided it tended to be
reasonably straightforward to fix. I have checked the code with these fixes
but to no avail.

I guess this is a reasonably common problem for anyone wanting to work with
Azure and needing to access the token?

If you have any suggestions on how to diagnose this error or better methods to
get the access key, kindly share please.


Answer: I found a solution. MS have a useful tool for debugging here:
<http://oauthdevconsole.cloudapp.net/PartialOAuth>

In the end the solution was that the headers needed to be converted to a
string and a workaround for an unusual character in the secret had to be
modified using the tool above.

The final code is here:

    
    
    def getTokens(grantType, scopeUrl, clientID, clientSecret, authUrl):
        conn = http.client.HTTPSConnection('datamarket.accesscontrol.windows.net')
        conn.request("POST", "/v2/OAuth2-13/", "client_id="+clientID+"&client_secret="+clientSecret+"&grant_type=client_credentials&scope="+scopeUrl)
        response = conn.getresponse()
        print(response.status, response.reason)
    

Best regards,

  * Rob

