Forums

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

API tokens works in CURL but Fails in Java Program

SVCA JIRA
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 5, 2018

I  use an API token to create an issue. when I use CURL the request is successful

curl -v https://mydomain.atlassian.net/rest/api/2/issue/ -H "Content-Type: application/json" -d @./datasample.json --user myuser@mydomain.com:apitokenhere

datasample.json:

{ "fields": { "project":{"key": "BAU"}, "summary": "Summary", "description": "description Here", "issuetype": {"name": "Story"}, "priority": {"name": "Low"}, "components": [{"name": "CORE_ALL"}], "labels": ["Sales"]}, "update":{ "issuelinks":[ { "add":{ "type":{ "name":"Relates", "inward":"relates to", "outward":"relates to" }, "outwardIssue":{ "key":"RFC-944" } } } ] }}

But I use the same payload in Java,  I get Invalid request 400 and the following error messages:

------------------------------------------------------

{

    "errorMessages": [],

    "errors": {

        "summary": "Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.",

        "components": "Field 'components' cannot be set. It is not on the appropriate screen, or unknown.",

        "description": "Field 'description' cannot be set. It is not on the appropriate screen, or unknown.",

        "issuelinks": "Field 'issuelinks' cannot be set. It is not on the appropriate screen, or unknown.",

        "priority": "Field 'priority' cannot be set. It is not on the appropriate screen, or unknown.",

        "labels": "Field 'labels' cannot be set. It is not on the appropriate screen, or unknown."

    }

}

---------------------------------------------------

i tested with very simple requests and no difference,  the fields are in the screen (It works in CURL)

 

JAVA Excerpt: 

String data="{ "fields": { "project":{"key""BAU"}, "summary""Summary", "description""description Here""issuetype": {"name""Story"}, "priority": {"name""Low"}, "components": [{"name""CORE_ALL"}], "labels": ["Sales"]}, "update":{ "issuelinks":[ { "add":{ "type":{ "name":"Relates", "inward":"relates to", "outward":"relates to" }, "outwardIssue":{ "key":"RFC-944" } } } ] }}"

String url="https://mydomain.atlassian.net/rest/api/2/issue/"

URL obj = new URL(url);

conn = (HttpURLConnection) obj.openConnection();

conn.setRequestProperty("Accept", "application/json");

conn.setRequestProperty("Content-Type", "application/json");

conn.setRequestMethod("POST");

conn.setRequestProperty("user","myuser@mydomain.com:apitokenhere");

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());

out.write(data);

out.close();

 

I was wondering if there a limitation on the token or I am doing something wrong??

Token generated as explain here: 

https://confluence.atlassian.com/cloud/api-tokens-938839638.html

 

 

 

 

 

1 answer

1 vote
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 7, 2018

Hi @SVCA JIRA,

I believe the problem here is with the below line:

conn.setRequestProperty("user","myuser@mydomain.com:apitokenhere");

 

When in Curl you pass the authentication credentials using the -u (or --user) argument, what Curl is doing is to base64 encoding the USERNAME:API_TOKEN string and adding the header "Authorization: Basic XXXXX" where XXXXX is the base64 encoded string.

This is the way HTTP Basic authentication works and it is explained in further details in the below page:

 

 

Therefore, in order to fix your code, please do the following:

  1. BASE64 encode the string myuser@mydomain.com:apitokenhere
  2. Supply an Authorization header with content Basic followed by the encoded string. For example, the string myuser@mydomain.com:apitokenhere encodes to ZnJlZDpmcmVk in base64, so you would replace the line:
    conn.setRequestProperty("user","myuser@mydomain.com:apitokenhere");
    with
    conn.setRequestProperty("Authorization: Basic ZnJlZDpmcmVk");

     

Suggest an answer

Log in or Sign up to answer