2013-06-03 55 views
0

我正嘗試使用REST API和Python在Windows Azure上創建服務。代碼如下:通過Python上的REST API創建Windows Azure服務

def create_service(self): 
      subscription_id = self.get_user_subscription_id() 
      auth = self.get_user_cert_data() 

      if auth is None or subscription_id is None: 
        return [(False, "Datos de autenticacion incorrectos")] 
      else: 
        key_file, cert_file = auth 

      service_name = str(int(time.time()*100)) 

      try: 

        conn = httplib.HTTPSConnection(self.AZURE_SERVER, self.AZURE_PORT, key_file=key_file, cert_file=cert_file) 
        uri = "https://%s/%s/services/hostedservices" % (self.AZURE_SERVER,subscription_id) 
        service_create_xml = ''' 
    <CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"> 
     <ServiceName>%s</ServiceName> 
     <Label>%s</Label> 
     <Description>Service %s created by the IM</Description> 
     <Location>West Europe</Location> 
    </CreateHostedService> 
        ''' % (service_name, base64.b64encode(service_name), service_name) 
        conn.request('POST', uri, body = service_create_xml, headers = {'x-ms-version' : '2013-03-01', 'ContentType' : 'application/xml'}) 
        resp = conn.getresponse() 
      except Exception, ex: 
        self.logger.exception("Error creando el service") 
        return None 

      if resp.status != 201: 
        self.logger.error("Error creando el service: Codigo " + str(resp.status) + " Reason: " + str(resp.reason)) 
        return None 

      return service_name 

但是,由於某些未知原因,響應總是錯誤400錯誤請求。任何人都知道我做錯了什麼?提前致謝。

回答

1

對於頭位置:

頭= { 'X-MS-版本': '2013-03-01', '的ContentType': '應用/ XML'}

嘗試使用Content-Type,而不是ContentType

+0

哦,你 '救' 我的生活獲得。我無法檢測到該錯誤。現在工作完美。非常感謝。 –

+0

:)。樂意效勞!您可能需要將它們標記爲答案。 –

+0

完成! (在兩個問題中)。對不起,我是新的Stackoverflow –

相關問題