Forums

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

curl API call to add a new label failing authentication (when using token)

romakarol January 17, 2022

The below command requests my password and then succeeds.


curl -u MyUsername -p -X PUT -H "Content-Type: application/json" --data '{"update":{"labels":[{"add":"test"}]}}' MyURL:8080/rest/api/latest/issue/TicketID

Following the basic authentication example. I would expect this to work using a token but I receive an access denied message.

curl -u MyEMAIL:MyToken -p -X PUT -H "Content-Type: application/json" --data '{"update":{"labels":[{"add":"test"}]}}' MyURL:8080/rest/api/latest/issue/TicketID

.

- (I also tried with username instead of email)
- The token was generated from my profile on the server. The username and email are also taken as shown on my profile.

Can anyone help? Ive also tried it this way (https://community.atlassian.com/t5/Jira-questions/How-to-authenticate-to-Jira-REST-API-with-curl/qaq-p/1312165), so it seems like the token does not work. Considering there's no options when generating one, is there really anything else to it beyond 'generate, copy, paste' that could cause an issue?

3 answers

1 accepted

1 vote
Answer accepted
Vishwas
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.
January 17, 2022

Hey @romakarol 

Welcome to Atlassian Community !!

Is your application hosted on cloud or on-premise. If it is cloud MyURL:8080 is not correct.

You have to use the cURL command like this:

curl --request GET \ --url 'https://your-domain.atlassian.net/rest/api/3/issue/{issueIdOrKey}' \ --user 'email@example.com:<api_token>' \ --header 'Accept: application/json'

 

Please check the Jira Cloud API doc here https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-get

In case of server you may have to try like this :

curl -u username:password -X POST --data @data.txt -H "Content-Type: application/json" http://localhost:8080/jira/rest/api/2/issue/{IssueKey}

Try with username and password you use to login to jira

Write data.txt file with label values like this:

{"update":{"labels":[{"add":"test"}]}}

Regards,

Vishwas

romakarol January 18, 2022
curl -u username:password -X POST --data @data.txt -H "Content-Type: application/json" http://localhost:8080/jira/rest/api/2/issue/{IssueKey}

The above works for me if that tells you anything about my deployment type. The 8080 port is necessary and works for our docker-deployed test JIRA environment - you can think of it as just part of the url but I'm providing the curl commands that actually worked for me.

 

username:password is ok, but I really would prefer to use a token for added security if possible. Is this really the only solution? According to the docs, as I mentioned, essentially this but username:token should have worked for me.

Vishwas
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.
January 18, 2022

Hey @romakarol 

Got your point.

From your explanation looks like you are on server/dc hosted plaform.

In that case out of the box api token based authentication is not there yet, we may have to rely on other 2 methods of Authorization that is OAuth and Cookie based method.

Please check this link out to understand more on OAuth based authentication 

https://community.atlassian.com/t5/Jira-questions/API-Tokens-for-self-hosted-Jira/qaq-p/820644

 

Regards,

Vishwas

Like Kishan Sharma likes this
2 votes
Kishan Sharma
Community Champion
January 17, 2022

Hi @romakarol Welcome to the Atlassian Community!

Can you try adding below header to your curl request and see if that works ?

-H "Authorization: Basic TOKEN" \

Make sure to replace TOKEN above with Base64 encoded token from your email and api token. Refer Supply basic auth headers for details.

romakarol January 18, 2022

I have, unfortunately this doesnt seem to work for me. The query stops working when username:password is replaced with the above.

Kishan Sharma
Community Champion
January 18, 2022

@romakarol  Since you tagged Jira-Cloud in your question, I was assuming that you were looking for solution on Cloud.

Following this link I was able to construct and send basic authorization header as follows on my DC version -

curl -H "Authorization: Basic ZnJlZDpmcmVk" -X GET -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/createmeta

You can try and see if that works for you too.

Kishan Sharma
Community Champion
January 19, 2022

Hi @romakarol did you get chance to try the solution I provided earlier ?

romakarol January 20, 2022

Thanks Im assuming 'Zn....VK' is the base 64 encoding of "username:token"? Will try now I had taken Vishwas' answer at face value that this isnt possible.

romakarol January 20, 2022

Reading your link it says to encode username:password

This is essentially what Im already doing, the problem was authorizing specifically with a token

Kishan Sharma
Community Champion
January 20, 2022

@romakarol - base64 encoding string is of the form username:password not username:token. If you are trying the latter, then it won't work.

1 vote
Pramodh M
Community Champion
January 17, 2022

Hi @romakarol 

Welcome to the Community!!

There's a difference between Server and Cloud API Authentication ways. I'm guessing your query is about Server

Just try with username and password and you should be able to authenticate to the API and let me know if works

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events