Hi there,
I have a question regarding the REST API. I have implemented a REST endpoint with ScriptRunner and want to acces the REST API when this endpoint gets called.
Basically I need get the renderedFields by using https://host.com/rest/api/2/issue/{issueIdD}?expand=renderedFields. Everything works fine.
My problem is that I want to use the credentials of the currently logged in user who calls this endpoint. At the moment the username and password for accessing the API are hard coded in the script what I don't really like. Is there any possibility to use the credentials of a logged in user?
Best regards,
Basit
@Nir Haimov fyi
Hi @Basit Rehman
Did you tried without credentials?
Maybe it will recognize that a user already loged in and will use the session automatically.
Hi @Nir Haimov
thanks for your reply.
I actually tried to call the API without credentials but I alwas got 401 as response code (unauthorized). But I am not sure if I used everything correctly. This is my approach:
//def login = "username:password"//def encoded = new String(Base64.getEncoder().encode(login.getBytes()))def obj = new URL(baseUrl + "/rest/api/2/issue/" + issueId + "?expand=renderedFields")def urlConnection = (HttpsURLConnection) obj.openConnection()urlConnection.setRequestMethod("GET")urlConnection.doOutput = true//urlConnection.setRequestProperty("Authorization", "Basic " + encoded.toString())log.info("connection established")BufferedReader bf = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))String inputLineStringBuffer response = new StringBuffer()while ((inputLine = bf.readLine()) != null) { response.append(inputLine)}bf.close()log.info(response)
As you can see the login data is commented out. This is the log:
INFO [runner.ScriptRunnerImpl]: connection established WARN [common.UserScriptEndpoint]: Script console script failed: java.io.IOException: Server returned HTTP response code: 401 for URL: https://{host}/rest/api/2/issue/{issueId}?expand=renderedFields at Script34.run(Script34.groovy:39)
Is there anything else I have to consider?
Regards,
Hi @Basit Rehman,
why are you trying to access Jira from inside Jira (scriptrunner) via rest api?
you should access everything directly with the JAVA API and not rest api.
Please tell me what you are trying to do so i will be able to guide you better.
Hi,
basically I created an REST endpoint that has to look for (rendered) comments in a given timeframe. A user presses a certain button in JIRA that opens a dialog. In this dialog you have to enter starting time and end time so the script can print all the relevant comments on a generated html page.
I have also tried to get the comments by using the Jira Java API but it doesn't give me the rendered fields... I have only found the getBody() method in com.atlassian.jira.issue.comments.Comment which doesn't help me really.
def commentManager = ComponentAccessor.getCommentManager()def comments = commentManager.getComments(issueId) //list of comments
I hope you understand my problem and would be very glad if you could help me out
It looks like you're new here. Sign in or register to get started.