使用python和suds在AutoTask中创建任务

时间:2013-09-04 22:27:32

标签: python soap suds

我正在研究一个必须通过SOAP API在AutoTask中创建任务的内部应用程序。该应用程序是用python编写的,我们使用suds进行SOAP通信,代码如下:

url = AUTOTASK_API_URL
t = HttpAuthenticated(username=AUTOTASK_API_USERNAME, password=AUTOTASK_API_PASSWORD)
client = Client(url, transport=t)

task = self.client.factory.create('Task')

task.ProjectID = PROJECT_ID
task.Title = 'title test'
task.StartDateTime = datetime.datetime.now()
task.EndDateTime =  datetime.datetime.now() + datetime.timedelta(days=1)
task.DepartmentID = DEPARTMENT_ID
task.TaskType= 2
task.Status = 1
task.EstimatedHours = 8.0
task.Priority = 0
task.IsVisibleInClientPortal = False

taskArray = self.client.factory.create('ArrayOfEntity')
taskArray.Entity = [task]

response = self.client.service.create([taskArray])

但是这段代码抛出了这个异常:

ERROR    2013-09-04 17:06:22,921 client.py:656] <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://autotask.net/ATWS/v1_5/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:create>
         <ns0:Entities>
            <ns0:Entity xsi:type="ns0:Task">
               <ns0:id/>
               <ns0:ProjectID>[PROJECT_ID]</ns0:ProjectID>
               <ns0:Title>title test</ns0:Title>
               <ns0:StartDateTime>2013-09-04 17:06:20.434000</ns0:StartDateTime>
               <ns0:EndDateTime>2013-09-05 17:06:20.434000</ns0:EndDateTime>
               <ns0:DepartmentID>[DEPARTMENT_ID]</ns0:DepartmentID>
               <ns0:TaskType>2</ns0:TaskType>
               <ns0:Status>1</ns0:Status>
               <ns0:EstimatedHours>8.0</ns0:EstimatedHours>
               <ns0:Priority>0</ns0:Priority>
               <ns0:IsVisibleInClientPortal>False</ns0:IsVisibleInClientPortal>
            </ns0:Entity>
         </ns0:Entities>
      </ns0:create>
   </ns1:Body>
</SOAP-ENV:Envelope>

INFO     2013-09-04 17:06:22,927 views_test.py:8] Starting 

Error
Traceback (most recent call last):
  File "helpers.py", line 21, in add_task
    response = self.client.service.create([taskArray])
  File "suds\client.py", line 542, in __call__
    return client.invoke(args, kwargs)
  File "suds\client.py", line 602, in invoke
    result = self.send(soapenv)
  File "suds\client.py", line 657, in send
    result = self.failed(binding, e)
  File "suds\client.py", line 712, in failed
    r, p = binding.get_fault(reply)
  File "suds\bindings\binding.py", line 265, in get_fault
    raise WebFault(p, faultroot)
WebFault: Server raised fault: 'System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (1, 364). ---> System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Xml.XmlConvert.ToInt64(String s)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read70_Task(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read5_Entity(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read136_create()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer96.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()'

2 个答案:

答案 0 :(得分:0)

问题在于自定义字段是必须的。

尝试删除此字段。

干杯:p

答案 1 :(得分:0)

问题是,即使我想创建一个新字段,也必须填写ID字段,在这种情况下,ID的值应为0