Forums

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

Server returned HTTP response code: 401

Balamurugan Maruthakani
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!
February 15, 2019

Hi Team,

 

I am trying to log a defect into JIRA system using JAVA. But I always get 401 unauthorized error. Even I tried with API token but still same result

please help me out on this. My code is below

package com.core.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import javax.json.*;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.testng.Assert;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


import org.apache.commons.codec.binary.Base64;

 

public class TestClass {

public static JsonObject jsonObj;

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

try {


URL url = new URL("https://myusername:APIToken@CompanyJIRAURL/rest/api/2/issue/");

System.out.println(url.getUserInfo());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

String encodedData = getJSON_Body();
System.out.println(encodedData);

System.out.println(getJSON_Body());
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64(url.getUserInfo().getBytes()));
conn.setRequestProperty("Content-Type", "application/json");
conn.getOutputStream().write(encodedData.getBytes());

try {
InputStream inputStream = conn.getInputStream();
System.out.println(inputStream);
} catch (IOException e) {
System.out.println(e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}

 

private static String getJSON_Body(){
javax.json.JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key", "ProjectKey"))
.add("summary", "Test Story")
.add("description", "Test Story")
.add("issuetype",
Json.createObjectBuilder().add("name", "Bug"))
).build();

return createIssue.toString();
}
}

1 answer

1 accepted

3 votes
Answer accepted
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 15, 2019

Hi there,

I think that you cannot put the username and api token in the URL.

Instead, try placing it in the Authorization header field.

I hope that helps!

Balamurugan Maruthakani
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!
February 19, 2019

Thanks Martyn. It worked

christian chakoua
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!
June 27, 2019

Bonjour Balamurugan;

 

Je peux voir  le code de ce que tu as fait pour que ça marche stp ?

Suggest an answer

Log in or Sign up to answer
TAGS
loom essentials certification, loom training, get certified in loom, atlassian certification, loom async video, video messaging skills, atlassian learning, loom for teams, online certification, loom badge, loom for business, atlassian education

Get Certified in Loom! 🧑‍🎓

Time to up your Loom game! The new Loom Essentials Certification is here! Show off your skills, learn pro tips, and get officially recognized. Perfect for taking your video messaging to the next level.

Learn more
AUG Leaders

Atlassian Community Events