我正在尝试使用http post方法在servicenow中创建一个事件。我使用HTTP post方法,通过使用JSON发送事件创建数据。 如下:
HttpPost httpPost = new HttpPost("https://myInstance.service-now.com/api/now/table/incident");
String postData = " {\n" +
" \"upon_approval\": \"\",\n" +
" \"location\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/cmn_location\\/1083361cc611227501b682158cabf646\",\n" +
" \"value\": \"1083361cc611227501b682158cabf646\"\n" +
" },\n" +
" \"expected_start\": \"\",\n" +
" \"reopen_count\": \"\",\n" +
" \"close_notes\": \"Closed before close notes were made mandatory\",\n" +
" \"impact\": \"1\",\n" +
" \"urgency\": \"1\",\n" +
" \"correlation_id\": \"\",\n" +
" \"sys_tags\": \"\",\n" +
" \"sys_domain\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user_group\\/global\",\n" +
" \"value\": \"global\"\n" +
" },\n" +
" \"description\": \"User can't access email on mail.company.com.\",\n" +
" \"group_list\": \"\",\n" +
" \"priority\": \"1\",\n" +
" \"delivery_plan\": \"\",\n" +
" \"sys_mod_count\": \"21\",\n" +
" \"work_notes_list\": \"\",\n" +
" \"follow_up\": \"\",\n" +
" \"closed_at\": \"2015-10-29 23:10:06\",\n" +
" \"sla_due\": \"\",\n" +
" \"delivery_task\": \"\",\n" +
" \"sys_updated_on\": \"2016-01-28 20:16:07\",\n" +
" \"parent\": \"\",\n" +
" \"work_end\": \"\",\n" +
" \"number\": \"INC0009871\",\n" +
" \"closed_by\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user\\/9ee1b13dc6112271007f9d0efdb69cd0\",\n" +
" \"value\": \"9ee1b13dc6112271007f9d0efdb69cd0\"\n" +
" },\n" +
" \"work_start\": \"\",\n" +
" \"calendar_stc\": \"7937181\",\n" +
" \"business_duration\": \"1970-01-22 21:46:21\",\n" +
" \"category\": \"network\",\n" +
" \"incident_state\": \"7\",\n" +
" \"activity_due\": \"\",\n" +
" \"correlation_display\": \"\",\n" +
" \"company\": \"\",\n" +
" \"active\": \"false\",\n" +
" \"due_date\": \"\",\n" +
" \"assignment_group\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user_group\\/d625dccec0a8016700a222a0f7900d06\",\n" +
" \"value\": \"d625dccec0a8016700a222a0f7900d06\"\n" +
" },\n" +
" \"caller_id\": \"\",\n" +
" \"knowledge\": \"false\",\n" +
" \"made_sla\": \"false\",\n" +
" \"comments_and_work_notes\": \"\",\n" +
" \"parent_incident\": \"\",\n" +
" \"state\": \"7\",\n" +
" \"user_input\": \"\",\n" +
" \"sys_created_on\": \"2014-05-29 18:24:13\",\n" +
" \"approval_set\": \"\",\n" +
" \"reassignment_count\": \"1\",\n" +
" \"rfc\": \"\",\n" +
" \"child_incidents\": \"\",\n" +
" \"opened_at\": \"2015-10-28 23:09:51\",\n" +
" \"short_description\": \"Can't read email\",\n" +
" \"order\": \"\",\n" +
" \"sys_updated_by\": \"admin\",\n" +
" \"resolved_by\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user\\/6816f79cc0a8016401c5a33be04be441\",\n" +
" \"value\": \"6816f79cc0a8016401c5a33be04be441\"\n" +
" },\n" +
" \"notify\": \"1\",\n" +
" \"upon_reject\": \"\",\n" +
" \"approval_history\": \"\",\n" +
" \"problem_id\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/problem\\/9d3a266ac6112287004e37fb2ceb0133\",\n" +
" \"value\": \"9d3a266ac6112287004e37fb2ceb0133\"\n" +
" },\n" +
" \"work_notes\": \"\",\n" +
" \"calendar_duration\": \"1970-04-02 20:46:21\",\n" +
" \"close_code\": \"Closed\\/Resolved by Caller\",\n" +
" \"sys_id\": \"9c573169c611228700193229fff72400\",\n" +
" \"approval\": \"\",\n" +
" \"caused_by\": \"\",\n" +
" \"severity\": \"1\",\n" +
" \"sys_created_by\": \"pat\",\n" +
" \"resolved_at\": \"2016-01-28 19:56:12\",\n" +
" \"assigned_to\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user\\/46b87022a9fe198101a78787e40d7547\",\n" +
" \"value\": \"46b87022a9fe198101a78787e40d7547\"\n" +
" },\n" +
" \"business_stc\": \"1892781\",\n" +
" \"sys_domain_path\": \"\\/\",\n" +
" \"cmdb_ci\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/cmdb_ci\\/b0c4030ac0a800090152e7a4564ca36c\",\n" +
" \"value\": \"b0c4030ac0a800090152e7a4564ca36c\"\n" +
" },\n" +
" \"opened_by\": {\n" +
" \"link\": \"https:\\/\\/dev13413.service-now.com\\/api\\/now\\/table\\/sys_user\\/681ccaf9c0a8016400b98a06818d57c7\",\n" +
" \"value\": \"681ccaf9c0a8016400b98a06818d57c7\"\n" +
" },\n" +
" \"subcategory\": \"\",\n" +
" \"sys_class_name\": \"incident\",\n" +
" \"watch_list\": \"\",\n" +
" \"time_worked\": \"\",\n" +
" \"contact_type\": \"\",\n" +
" \"escalation\": \"0\",\n" +
" \"comments\": \"\"\n" +
" }";
我得到一个错误响应输出如下: 我不确定创建事件中的必填字段是什么。 HTTP / 1.1 403禁止 {"错误":{"消息":"操作失败","详细信息":"插入事件期间出错(INC0009871) )"}"状态":"失效"}
答案 0 :(得分:1)
403错误意味着您未将凭据传递到端点。您好像缺少身份验证标头
在第1行后尝试以下内容:
String encoding = Base64Encoder.encode ("<%your username%>:<%your password%>");
httpPost.setHeader("Authorization", "Basic " + encoding);