Not able to access jira cloud data by using JAVA

Getting 401 error:-

My Code:-

import com.sun.org.apache.xml.internal.security.utils.Base64;

import javax.json.Json;
import javax.json.JsonObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class testREST_CreateIssue {
    public static void main(String[] args) {
        try {
            URL jiraREST_URL = new URL("https://crgsolutions2.atlassian.net/projects/CRGWEB/issues/");
            URLConnection urlConnection = jiraREST_URL.openConnection();
            urlConnection.setDoInput(true);

            HttpURLConnection conn = (HttpURLConnection) jiraREST_URL.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);

            String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

            System.out.println(getJSON_Body() + "/" + encodedData);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "Basic " + Base64.encode("basavarajajuttiyavar@gmail.com:CRGTOKEN".getBytes(), 0));
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
            conn.getOutputStream().write(encodedData.getBytes());

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

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getJSON_Body(){
        JsonObject createIssue = Json.createObjectBuilder()
                .add("fields",
                        Json.createObjectBuilder().add("CRGWebApplication",
                                Json.createObjectBuilder().add("key","CRGWEB-1"))
                                .add("summary", "sum")
                                .add("description", "descr")
                                .add("issuetype",
                                        Json.createObjectBuilder().add("id", "10105"))
                ).build();

        return createIssue.toString();

    }
}

I am getting the below error:-

===============================

{“fields”:{“CRGWebApplication”:{“key”:“CRGWEB-1”},“summary”:“sum”,“description”:“descr”,“issuetype”:{“id”:“10105”}}}/%7B%22fields%22%3A%7B%22CRGWebApplication%22%3A%7B%22key%22%3A%22CRGWEB-1%22%7D%2C%22summary%22%3A%22sum%22%2C%22description%22%3A%22descr%22%2C%22issuetype%22%3A%7B%22id%22%3A%2210105%22%7D%7D%7D

Server returned HTTP response code: 401 for URL: https://crgsolutions2.atlassian.net/projects/CRGWEB/issues/

Hello @BasavarajaJuttiyavar,

If you’re looking into creating an issue, the proper REST API to use is POST /rest/api/3/issue. In your case, try changing the parameter in your URL constructor, to something like

URL jiraREST_URL = new URL("https://crgsolutions2.atlassian.net/rest/api/3/issue");

Do try it out and tell us how it goes.

Cheers,
Ian

Still i have the following issue:-

{"fields":{"CRGWebApplication":{"key":"CRGWEB-2"},"summary":"sum","description":"descr","issuetype":{"id":"10105"}}}/%7B%22fields%22%3A%7B%22CRGWebApplication%22%3A%7B%22key%22%3A%22CRGWEB-2%22%7D%2C%22summary%22%3A%22sum%22%2C%22description%22%3A%22descr%22%2C%22issuetype%22%3A%7B%22id%22%3A%2210105%22%7D%7D%7D
Server returned HTTP response code: 401 for URL: https://crgsolutions2.atlassian.net/rest/api/3/issue

Process finished with exit code 0

In my understanding of your code, getJSON_Body returns the above string. What is the field CRGWebApplication? Is it a custom field? Prior to creating an issue, kindly check the allowed fields for the project by calling Get create issue metadata. The basic project without modifications usually accepts the following request body at the bare minimum:

{
  "fields": {
    "project": {
      "id": "10000"
    },
    "summary": "This is the summary",
    "description": "hello description",
    "issuetype": {
      "id": "10001"
    }
  }
}

Stepping back a bit, since you’re getting a 401, this looks like an authorization problem. By any chance are you already using API tokens (are you just using CRGTOKEN as a substitute in this post)?

Thanks,
Ian

Thanks for the quick response Ian,

I have created this (link: Atlassian account)to get access to Jira cloud to create an issue…

Regards,
Basu

This is interesting. Before deep diving into the Java code, I suggest let’s isolate the issue that’s causing the 401. Can you do a successful cURL (or something like Postman if you prefer) call to /rest/api/3/myself using your email and API token? The request should look something like

curl -u basavarajajuttiyavar@gmail.com:<apiToken> https://crgsolutions2.atlassian.net/rest/api/3/myself

If this is successful, we can now rule out the possibility of incorrect credentials and then deep dive to the code.


Hi Lan,

By using Https link :https://crgsolutions2.atlassian.net/rest/api/3/myself, i am able to login with token by using postman…

But your screenshot shows a 401 Unauthorized result?

If this is the URL that you’re using, it isn’t a valid API call, it needs to be of the form

https://crgsolutions2.atlassian.net/rest/api/3/xxx

where xxx is something like issue or search

See the API documentation

Thanks Levy,

I am using the link as “https://crgsolutions2.atlassian.net/rest/api/3/issue”

Still i am unable to authenticate to do create issue in Jira cloud.

Any help is really appreciated!

Regards,
Basu

@BasavarajaJuttiyavar, I am not sure if you were able to make a successful call since you got a 401 Unauthorized response in your screenshot.

Once you get a positive response in the /myself endpoint, you can also try creating an issue via Postman using the same credentials.

@BasavarajaJuttiyavar let’s continue the discussion here. Can you respond to the question as to whether you were able to get a 200 OK response from https://crgsolutions2.atlassian.net/rest/api/3/myself using Postman?

Thanks Remie,

Hi Lan,

I am pasting the link “https://crgsolutions2.atlassian.net/rest/api/3/myself” in Parms filed of post man and in Authorization filed i am selecting as Basic Auth and using jira cloud login name as username and password as token generated from the link: [Atlassian account ] in postman. But still i am getting 401 error i am not sure where i am missing!

Just to be clear, you have used postman and entered the following information:

GET https://crgsolutions2.atlassian.net/rest/api/3/myself
Authorization: Basic Auth
Username: basavarajajuttiyavar@gmail.com
Password: API TOKEN

(in the screenshot you provided, the username seems to have 3 dots behind it basavarajajuttiyavar@gmail.com…)

If this is true, are you sure that your user has access to this Jira instance?

Hi Ramie,

CodeSnip:-

URL jiraREST_URL = new URL("https://crgsolutions10.atlassian.net/rest/api/3/issue");
            URLConnection urlConnection = jiraREST_URL.openConnection();
            urlConnection.setDoInput(true);

            HttpURLConnection conn = (HttpURLConnection) jiraREST_URL.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);

            String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

           System.out.println(getJSON_Body() + "/" + encodedData);
            //conn.setRequestMethod("POST");
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Basic " + Base64.encode("juttiyavar1972@gmail.com:".getBytes(), 0));
            System.out.println("Looged in");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
            conn.getOutputStream().write(encodedData.getBytes());

Output:-

{“fields”:{“CRGWebApplication”:{“key”:“CRGWEB-3”},“summary”:“sum”,“issuetype”:{“id”:“10000”}}}/%7B%22fields%22%3A%7B%22CRGWebApplication%22%3A%7B%22key%22%3A%22CRGWEB-3%22%7D%2C%22summary%22%3A%22sum%22%2C%22issuetype%22%3A%7B%22id%22%3A%2210000%22%7D%7D%7D
Looged in
Server returned HTTP response code: 400 for URL: https://crgsolutions10.atlassian.net/rest/api/3/issue

Process finished with exit code 0

Executed by recreating new cloud login…

Hi @BasavarajaJuttiyavar

That API call isn’t valid, you need to specify an issue key so it would look like

URL jiraREST_URL = new URL(“https://crgsolutions10.atlassian.net/rest/api/3/issue/ABC-123

@BasavarajaJuttiyavar

You may want to edit your last post and remove your login details - this is a public site

Thanks levy, I removed the passcode…

Now the out put is as below:-

{“fields”:{“CRGWebApplication”:{“key”:“CRGWEB-3”},“summary”:“sum”,“issuetype”:{“id”:“10000”}}}/%7B%22fields%22%3A%7B%22CRGWebApplication%22%3A%7B%22key%22%3A%22CRGWEB-3%22%7D%2C%22summary%22%3A%22sum%22%2C%22issuetype%22%3A%7B%22id%22%3A%2210000%22%7D%7D%7D
Looged in
Server returned HTTP response code: 405 for URL: https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-3

Process finished with exit code 0

@BasavarajaJuttiyavar

Does CRGWEB-3 exist? Do you have access to it? Have you tried a more general call, like

https://crgsolutions10.atlassian.net/rest/api/3/search

which will return all items?

Or even

https://crgsolutions10.atlassian.net/rest/api/3/myself

which just returns your details