Product REST API - Authentication error

i tried to create a issue using rest api

import api, { route } from "@forge/api";
export async function createTicket(context) {
	console.log('Ticket Created Web Trigger');
    console.log("context",context);
    var bodyData = `{
        "fields": {
          "assignee": {
            "id": "5b109f2e9729b51b54dc274d"
          },
          "components": [
            {
              "id": "10000"
            }
          ],
          "customfield_10000": "09/Jun/19",
          "customfield_20000": "06/Jul/19 3:25 PM",
          "customfield_30000": [
            "10000",
            "10002"
          ],
          "customfield_40000": {
            "content": [
              {
                "content": [
                  {
                    "text": "Occurs on all orders",
                    "type": "text"
                  }
                ],
                "type": "paragraph"
              }
            ],
            "type": "doc",
            "version": 1
          },
          "customfield_50000": {
            "content": [
              {
                "content": [
                  {
                    "text": "Could impact day-to-day work.",
                    "type": "text"
                  }
                ],
                "type": "paragraph"
              }
            ],
            "type": "doc",
            "version": 1
          },
          "customfield_60000": "jira-software-users",
          "customfield_70000": [
            "jira-administrators",
            "jira-software-users"
          ],
          "customfield_80000": {
            "value": "red"
          },
          "description": {
            "content": [
              {
                "content": [
                  {
                    "text": "Order entry fails when selecting supplier.",
                    "type": "text"
                  }
                ],
                "type": "paragraph"
              }
            ],
            "type": "doc",
            "version": 1
          },
          "duedate": "2019-05-11",
          "environment": {
            "content": [
              {
                "content": [
                  {
                    "text": "UAT",
                    "type": "text"
                  }
                ],
                "type": "paragraph"
              }
            ],
            "type": "doc",
            "version": 1
          },
          "fixVersions": [
            {
              "id": "10001"
            }
          ],
          "issuetype": {
            "id": "10000"
          },
          "labels": [
            "bugfix",
            "blitz_test"
          ],
          "parent": {
            "key": "PROJ-123"
          },
          "priority": {
            "id": "20000"
          },
          "project": {
            "id": "10000"
          },
          "reporter": {
            "id": "5b10a2844c20165700ede21g"
          },
          "security": {
            "id": "10000"
          },
          "summary": "Main order flow broken",
          "timetracking": {
            "originalEstimate": "10",
            "remainingEstimate": "5"
          },
          "versions": [
            {
              "id": "10000"
            }
          ]
        },
        "update": {}
      }`;
      
      const response = await api.asUser().requestJira(route`/rest/api/3/issue`, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: bodyData
      });
      
      console.log(`Response: ${response.status} ${response.statusText}`);
      console.log(await response.json());
}

i am getting this error

ERROR   07:24:45.957  1e6dcbafdca3e95cbdeff22d70b67349  [NEEDS_AUTHENTICATION_ERR: Authentication required] {
  serviceKey: 'atlassian-token-service-key',
  status: 401
}

where to get ‘atlassian-token-service-key’,and how to add it in header
please help me

thanks in advace

If you’re using this in a web trigger then you need to call the API asApp(). asUser() isn’t supported. The error message might just be a bit misleading.

1 Like