Server returned HTTP response code: 401

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

Thanks Martyn. It worked

christian chakoua 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
AUG Leaders

Atlassian Community Events