ServiceNow - 如何使用 SOAP 下载报告

ServiceNow - How to use SOAP to download reports

我需要从 serviceNow 自动下载报告。

我已经能够通过以下方法使用 pythonselenium 以及 win32com 使其自动化。
https://test.service-now.com/sys_report_template.do?CSV&jvar_report_id=92a....7aa

并使用 selenium 访问 serviceNow 并修改 firefox 默认下载选项以将文件转储到 windows 机器上的文件夹。

但是,由于所有这些都可以移植到 linux 服务器,我们希望将其移植到 SOAPCURL

我遇到了 serviceNow 个用于 python here 的库。

我试过了,如果我使用以下 ServiceNow.py

class Change(Base):
    __table__ = 'change_request.do'

并在 site.

中列出的客户端脚本中执行
# Fetch changes updated on the last 5 minutes
changes = chg.last_updated(minutes=5)
#print changes client side script.

for eachline in changes:
    print eachline

但是,当我用 sys_report_template.do 替换 URL 时,出现错误

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\SOAPpy\Parser.py", line 1080, in _parseSOAP
    parser.parse(inpsrc)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "C:\Python27\Lib\xml\sax\xmlreader.py", line 125, in parse
    self.close()
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 220, in close
    self.feed("", isFinal = 1)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 214, in feed
    self._err_handler.fatalError(exc)
  File "C:\Python27\Lib\xml\sax\handler.py", line 38, in fatalError
    raise exception
SAXParseException: <unknown>:1:0: no element found

这是相关代码

from servicenow import ServiceNow
from servicenow import Connection
from servicenow.drivers import SOAP

# For SOAP connection
conn = SOAP.Auth(username='abc', password='def', instance='test')

rpt  = ServiceNow.Base(conn)
rpt.__table__ = "sys_report_template.do?CSV"

#jvar_report_id replaced with .... to protect confidentiality
report = rpt.fetch_one({'jvar_report_id': '92a6760a......aas'})

for eachline in report:
    print eachline

所以,我的问题是,如何才能使这项工作成功? 我在网上寻找资源和帮助,但没有找到。

感谢任何帮助。

经过大量研究,我能够使用以下方法从 servicenow 获取 csv 格式的报告。我想我会 post 过来,以防其他人遇到类似问题。

import requests
import json

# Set the request parameters
url= 'https://myinstance.service-now.com/sys_report_template.do?CSV&jvar_report_id=929xxxxxxxxxxxxxxxxxxxx0c755'
user = 'my_username'
pwd = 'my_password'

# Set proper headers
headers = {"Accept":"application/json"}

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )
response.raise_for_status()
print response.text

response.text 现在有 csv 格式的报告。
接下来我需要弄清楚,如何解析 response 对象以正确格式提取 csv 数据。
完成后,我会 post 过来。但现在这回答了我的问题。

我尝试了这个并且它按预期工作。 `进口请求 导入 json

url= 'https://myinstance.service-now.com/sys_report_template.do?CSV&jvar_report_id=929xxxxxxxxxxxxxxxxxxxx0c755' 用户 = 'my_username' 密码 = 'my_password'

response = requests.get(url, auth=(user, pwd), headers=headers )

file_name = "abc.csv"

with open(file_name, 'wb') as out_file: out_file.write(response.content) 删除响应`