Javascript code sample for authentication with REST API

Prasad August 15, 2019

Hi,

Could you please help me for some javascript code sample for authentication with REST API?

I am using JIRA - v7.1.10

1 answer

1 accepted

1 vote
Answer accepted
Andrew
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2019

Hi @Prasad ,

I use groovy:

class HttpProvider(){
private static String authString

private static credentional(){
Console cons
cons = System.console()
username = cons.read("[%s]", 'Username:')
password = cons.readPassword("[%s]", 'Password:')
this.authString = "${username}:${password}".getBytes().encodeBase64().toString()
}

private static get(String url) {
def connection = url.toURL().openConnection()
if (this.authString == null) this.credentional()
connection.addRequestProperty("Authorization", "Basic ${this.authString}")

connection.setRequestMethod("GET")
connection.doOutput = false
connection.connect()
connection.content.text
}

}

In groovy by default return last operator in method. In java it equals: return connection.content.text 

B.R.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2019

Hi @Prasad 

Just to add a bit to @Andrew answer, the username needs to be the Jira e-mail address and the password needs to be the API token, as per this post

Prasad August 16, 2019

Hi @Andrew and @Warren .

Thank you for answer. I want to know whether we cannot use plane JavaScript for authentication?

Suggest an answer

Log in or Sign up to answer