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();
}
}
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bonjour Balamurugan;
Je peux voir le code de ce que tu as fait pour que ça marche stp ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.