AWS EC2 中的 Python 脚本如何与 ServiceNow REST 通信 API

How a Python script in AWS EC2 can communicate to ServiceNow REST API

我正在尝试学习如何通过 REST API 通知 ServiceNow 我使用 Python 脚本(在 AWS EC2 Windows Server 2012) 获取更新的记录。我应该学习哪些特定的 Python 库/模块来让我朝着正确的方向发展?

目前我的 Python 脚本和 MySQL RDS 通信正常。

我仍处于尝试更好地了解 REST API 和 AWS EC2 的阶段。

任何其他可以共享的 AWS、ServiceNow 或 Python 相关信息将不胜感激。

ServiceNow table REST API 非常简单,因此使用 Python 将记录插入任意 table 是一件轻而易举的事。例如:

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://[instance name].service-now.com/api/now/table/[table name]'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request - this is fake data
response = requests.post(url, auth=(user, pwd), headers=headers ,data="[your json string of fields and values]")

# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

ServiceNow 中的 REST API Explorer 对于构建和测试查询非常有用。它甚至生成示例代码。您可以在导航器中搜索 REST API Explorer 来找到它。

另一种选择是在 ServiceNow 中创建一个 Scripted REST API 来创建一个自定义的 URL,您可以点击它来实现通知。如果您不需要持久化数据而只想收到通知,这很好。