Forums

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

Upload Attach - API Token - Java

zgrshn33
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!
December 28, 2018 edited

Hi

 

This code is working.

I can upload the attachment to the issue

String pathname= "screenshot-5.png";
File fileUpload = new File(pathname);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("https://XXX.atlassian.net/rest/api/2/issue/XXX-2/attachments");
BASE64Encoder base=new BASE64Encoder();
String encoding = base.encode ("username:password".getBytes());
postRequest.setHeader("Authorization", "Basic " + encoding);
postRequest.setHeader("X-Atlassian-Token","nocheck");
MultipartEntityBuilder entity=MultipartEntityBuilder.create();
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity( entity.build());
HttpResponse response = httpClient.execute(postRequest);
System.out.println(response);

But I don't want to use my user name and password in my code .

I get API Token for my user

Screenshot at Dec 28 17-17-24.png

but i cant set the API Key to the httppost, I get 

String pathname= "screenshot-5.png";
File fileUpload = new File(pathname);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("https://XXX.atlassian.net/rest/api/2/issue/XX-2/attachments");
postRequest.setHeader("Authorization", "Basic " + "OHi4Y7vowLxtgf456GNse4F0D");
postRequest.setHeader("X-Atlassian-Token","nocheck");
MultipartEntityBuilder entity=MultipartEntityBuilder.create();
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity( entity.build());
HttpResponse response = httpClient.execute(postRequest);
System.out.println(response);

 

The response:

HttpResponseProxy{HTTP/1.1 404 [Server: Atlassian Proxy/1.13.6.2, Vary: Accept-Encoding, Cache-Control: no-cache, no-store, no-transform, Content-Type: application/json;charset=UTF-8, Strict-Transport-Security: max-age=315360000; includeSubDomains; preload, Date: Fri, 28 Dec 2018 13:54:05 GMT, ATL-TraceId: 3184af167d55799b, X-AREQUESTID: 55a30772-41f4-4469-931c-319bf1a3ea3f, X-XSS-Protection: 1; mode=block, Transfer-Encoding: chunked, Timing-Allow-Origin: *, X-AUSERNAME: anonymous, X-Content-Type-Options: nosniff, Connection: keep-alive, Set-Cookie: atlassian.xsrf.token=BVKF-A7NK-S2TE-5SA6_c2be0850c60dbef5983ed946da34a44559cfbe58_lout; Path=/; Secure] org.apache.http.client.entity.DecompressingEntity@35dab4eb}

 

1 answer

1 accepted

0 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 2, 2019

The fact that your API token page screenshot has a 'last access time' value of Never indicates to me that you have never successfully made a REST API call using that token yet.   

And since your response code appears to have an HTTP error code of 404, this tends to indicate that either you're using the incorrect Cloud URL site, OR we have something else that is incorrect with the way the call is being made.    I'm not as familiar with the specific syntax you're using here, but I have used an API token to make curl REST calls to my own cloud site.  In that case, I used a syntax of

curl --request POST \
  --url 'https://example.atlassian.net/rest/api/3/issue/{IssueKey}/attachments' \
  --header 'Authorization: Basic {username:API_token base64 encoded string here}' \
  --header 'X-Atlassian-Token: nocheck' \
  -F "file=@/path/to/file/12345.pdf"

The other thing to note here:  The API token you generate is not the only thing you need to supply in that authorization Basic parameter.  You actually need to create a new string of username:api_token and then base64 encode that string and use that.  This is explained in https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/

Supplying basic auth headers

If you need to, you may construct and send basic auth headers yourself. To do this you need to perform the following steps:

  1. Generate an API token for Jira using your Atlassian Account: https://id.atlassian.com/manage/api-tokens.
  2. Build a string of the form username:api_token.
  3. BASE64 encode the string.
  4. Supply an Authorization header with content Basic followed by the encoded string. For example, the string fred:fred encodes to ZnJlZDpmcmVk in base64, so you would make the request as follows:
1
2
3
4
5
curl -D- \
   -X GET \
   -H "Authorization: Basic ZnJlZDpmcmVk" \
   -H "Content-Type: application/json" \
   "https://your-domain.atlassian.net/rest/api/2/issue/QA-31"

I could see how maybe if you just trying to use the token itself, you would get a 404 error like this because you haven't supplied the username in a way that Jira Cloud can understand what account it is coming from.

zgrshn33
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!
January 3, 2019

Thanks for your response.

Suggest an answer

Log in or Sign up to answer