Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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 edited

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
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events