【问题标题】:Not knowing where to start in using REST API for creating ServiceNow incidents不知道从哪里开始使用 REST API 创建 ServiceNow 事件
【发布时间】:2018-11-21 11:10:04
【问题描述】:

我需要更新一个用于使用 SOAP API 创建事件工单的网络表单。现在它需要通过 ServiceNow 与 REST API 一起使用,坦率地说,我不知道从哪里开始。

现有网络表单的工作方式如下: 填写表格并按下提交按钮后,“submit.js”被触发。该函数将网络表单中的所有信息发布到“soapclient.php”。这个soapclient.php 连接到SOAP 客户端并将所有数据传输到SOAP API。

“soapclient.php”如下所示:

<?php
    $wsdl =wsdl-server;
    $client = new SoapClient($wsdl, array('login' => "USER",
                                          'password' => "PW“,
                                          'trace'=> 1));

    $title = $_REQUEST['title'];
    $affuser = $_REQUEST['affuser'];
    $description = $_REQUEST['description'];
    $solutionstring = '';
    $solutioncode = '';
    $resolveimmediately = '';

 [[SEVERAL IF-ELSE STATEMENTS]]

    $request = array(
            'model'=>array(
                'keys'=>array(),
                'instance'=>array(
                    'registrationId' => ID,
                    'affectedUserId' => $affuser,
                    'serviceId' => $serviceId,
                    'affectedCiId' => '',
                    'priority' => '4',
                    'title' => $title,
                    'description' => "$description\n$timestring$errorstring$phonestring",
                    'resolveImmediately' => $resolveimmediately,
                    'solutionCode' => $solutioncode,
                    'solution' => $solutionstring
        )));

$response = $client->SubmitIntApiIncident($request);

我很确定我只需将“soapclient.php”更改为使用新的 REST API 而不是 SOAP API。但这是我不知道从哪里开始的部分。 REST API 使用 API 密钥 + 通用用户(我都有) - 但我不知道在哪里使用它们。

我拥有的唯一线索是我可以下载的 swagger.json 文件。这个有点像我正在寻找的结构,但我不知道如何使用它。

“swagger.json”如下所示:

{
  "swagger" : "2.0",
  "host" : "send-dev.servicenow.com",
  "basePath" : "/api/ServiceNow/devb/incident/v2.5",
  "schemes" : [ "https" ],
  "paths" : {
    "/incident/createIncidentMethod" : {
      "post" : {
        "description" : "Create incident\n",
        "operationId" : "POST /incident/createIncidentMethod",
        "parameters" : [ {
          "description" : "Create Incident",
          "required" : false,
          "in" : "body",
          "name" : "body",
          "schema" : {
            "properties" : {
              "header" : {
                "properties" : {
                  "transactionid" : {
                    "type" : "string"
                  },
                  "sourcesystemid" : ""{
                    "type" : "string"
                  },
                  "targetsystemid" : " "{
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "content" : {
                "properties" : {
                  "caller_id" : {
                    "type" : "string"
                  },
                  "category" : {
                    "type" : "string"
                  },
                  "subcategory" : {
                    "type" : "string"
                  },
                  "business_service" : {
                    "type" : "string"
                  },
                  "ci_name" : {
                    "type" : "string"
                  },
                  "impact" : {
                    "type" : "string"
                  },
                  "urgency" : {
                    "type" : "string"
                  },
                  "assignment_group" : {
                    "type" : "string"
                  },
                  "assigned_to" : {
                    "type" : "string"
                  },
                  "short_description" : {
                    "type" : "string"
                  },
                  "state" : {
                    "type" : "string"
                  },
                  "close_code" : {
                    "type" : "string"
                  },
                  "close_notes" : {
                    "type" : "string"
                  },
                  "service_offering" : {
                    "type" : "string"
                  },
                  "affected_user" : {
                    "type" : "string"
                  },
                  "description" : {
                    "type" : "string"
                  },
                  "correlation_id" : {
                    "type" : "string"
                  },
                  "ci_sysid" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "attachment" : {
                "properties" : {
                  "file_name" : {
                    "type" : "string"
                  },
                  "mime_type" : {
                    "type" : "string"
                  },
                  "base64string" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              }
            },
            "type" : "object"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "Success",
            "schema" : {
              "type" : "object"
            }
          },
          "401" : {
            "description" : "Not authorized",
            "schema" : {
              "type" : "object"
            }
          },
          "610" : {
            "description" : "User doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "611" : {
            "description" : "Group doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "612" : {
            "description" : "User is not member of the group.",
            "schema" : {
              "type" : "object"
            }
          },
          "613" : {
            "description" : "Incident State is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "405" : {
            "description" : "Not supported. Invalid parameters.",
            "schema" : {
              "type" : "object"
            }
          },
          "603" : {
            "description" : "Missing mandatory information.",
            "schema" : {
              "type" : "object"
            }
          },
          "614" : {
            "description" : "Incident Category is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "615" : {
            "description" : "Incident Subcategory is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "616" : {
            "description" : "Only one input is allowed. Configuration item Name or SysId.",
            "schema" : {
              "type" : "object"
            }
          },
          "606" : {
            "description" : "Record not found.",
            "schema" : {
              "type" : "object"
            }
          },
          "618" : {
            "description" : "CI name is not unique."
          },
          "608" : {
            "description" : "Impact is invalid. Impact must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          },
          "609" : {
            "description" : "Urgency is invalid. Urgency must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          }
        }
      }
    }
  },
  "info" : {
    "title" : "ServiceNow Incident v2.5",
    "description" : "This REST API provides methods to create/update/retrieve ITSM incident module data from ServiceNow. This API uses Basic authentication (plus API Key). If there is a need to consume from outside the network, there is another version of this API that is configured for two factor authentication.",
    "version" : "1.0.0",
    "x-summary" : "SNOW Incident API"
  }
}

如果有人能告诉我如何使用新 API 或如何更改 soapclient.php 以使用新 API,我将不胜感激。

最好的 蒂姆

【问题讨论】:

  • 看起来您的 swagger.json 中的 send-dev.servicenow.com 不可用 - 所以第一个问题 - 使用该 API 的工作服务在哪里?
  • 要“运行” swagger.json(可视化它并发送请求)使用 swagger-ui swagger.io/tools/swagger-ui(还有 dockerized 版本:hub.docker.com/r/swaggerapi/swagger-ui(使用 docker 轻松运行))
  • 您好,Kamil,感谢您的快速回答。我不想透露我在哪家公司工作,这是我在发布之前编辑源代码的原因。
  • 我可能不明白你的问题。你可以去editor.swagger.io把你的json(它有2个错误——提示:删除“”)放在左边——然后在右边查看请求。使用该 API,您只能发出一个请求 POST send-dev.servicenow.com/api/ServiceNow/devb/incident/v2.5/…
  • 所以你的 swagger.json 只描述了一个 API 请求。您需要询问 API 提供商如何“登录”该 API

标签: php rest api swagger servicenow


【解决方案1】:

您的swagger.json 不是有效的 JSON(但是它有一些易于删除的小错误 - 在第 23 行和第 26 行删除大括号前的 " ")。您可以在https://editor.swagger.io/ 中查看该文件。该文件仅描述一个请求:

POST http://send-dev.servicenow.com/api/ServiceNow/devb/incident/v2.5/incident/createIncidentMethod

要向 API 表单 php 发送请求(POST 正文中包含正确的 json),您可以使用 GUZZLE 库而不是 SoapClient(guzzle 有很好的文档)。

在 swagger.json 中你只有以下信息:

此 REST API 提供创建/更新/检索 ITSM 事件的方法 来自 ServiceNow 的模块数据。此 API 使用基本身份验证(加上 API 密钥)。如果需要从外网消费, 此 API 的另一个版本针对两个因素进行了配置 身份验证。

但没有细节 - 但是它使用了关键字"Basic Authentication"。因此,您可以尝试将以下标头添加到您的 POST 请求中:

Authorization: Basic username:password_base64

其中 username:password_base64 是您的 base64 凭据代码 - 例如用户 demo 并传递 p@55w0rd 您需要编码 base64 字符串“demo:p@55w0rd”并添加标题:

Authorization: Basic ZGVtbzpwQDU1dzByZA== 

(它应该与 http/ssl 一起使用,因为 base64 很容易解码)。如果这不起作用,那么您需要询问您的 API 提供商:如何“登录”到该 API(某些 API 只需要额外的路径参数,如 `?key=abc...xyz')。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多