Not able to access jira cloud data by using JAVA

Since you are using GET here (for https://crgsolutions10.atlassian.net/rest/api/3/issue), you’ll most likely get a status 405. If you’re not getting any status 401, that’s a good indication that you do not have auth problems anymore.

I modified your code a bit to get the issue CRGWEB-1, assuming the juttiyavar1972@gmail.com have access to it. Do try it out, don’t forget to add the missing API token in the auth part AND do not pass your encoded JSON body for this GET call.

URL jiraREST_URL = new URL("https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-1"); //assuming there's a CRGWEB-1 issue available to juttiyavar1972@gmail.com
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("GET");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("juttiyavar1972@gmail.com:<apiToken>".getBytes(), 0));
...

Outside Java, to test it out, you could simply do a

curl -u juttiyavar1972@gmail.com:apiToken https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-1

When you’re successful with the GET call and feeling confident to do the POST /rest/api/3/issue call, be sure to create the request body properly. Based on your getJSON_Body method, you’re adding what seems to be a CRGWebApplication field, but I am not sure if that’s a valid custom field. I’ve given a sample request body in my first comment for a basic Create issue call.

Cheers,
ian

1 Like

Thanks Ian,

Still i have have the following error:-

Server returned HTTP response code: 405 for

Process finished with exit code 0

By using postman i am able to get the details with the authorization credentials.

PostMan Out put:-

{
“expand”: “renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations”,
“id”: “10000”,
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/issue/10000”,
“key”: “CRGWEB-1”,
“fields”: {
“statuscategorychangedate”: “2019-07-11T11:09:57.754+0530”,
“issuetype”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/issuetype/10004”,
“id”: “10004”,
“description”: “A problem which impairs or prevents the functions of the product.”,
“iconUrl”: “https://crgsolutions10.atlassian.net/secure/viewavatar?size=medium&avatarId=10303&avatarType=issuetype”,
“name”: “Bug”,
“subtask”: false,
“avatarId”: 10303
},
“timespent”: null,
“project”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/project/10000”,
“id”: “10000”,
“key”: “CRGWEB”,
“name”: “CRGWebApplication”,
“projectTypeKey”: “software”,
“simplified”: false,
“avatarUrls”: {
“48x48”: “https://crgsolutions10.atlassian.net/secure/projectavatar?pid=10000&avatarId=10417”,
“24x24”: “https://crgsolutions10.atlassian.net/secure/projectavatar?size=small&s=small&pid=10000&avatarId=10417”,
“16x16”: “https://crgsolutions10.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10000&avatarId=10417”,
“32x32”: “https://crgsolutions10.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10000&avatarId=10417
}
},
“fixVersions”: ,
“aggregatetimespent”: null,
“resolution”: null,
“customfield_10027”: null,
“resolutiondate”: null,
“workratio”: -1,
“lastViewed”: “2019-07-16T15:39:59.264+0530”,
“watches”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-1/watchers”,
“watchCount”: 1,
“isWatching”: true
},
“created”: “2019-07-11T11:09:57.523+0530”,
“customfield_10020”: null,
“customfield_10021”: null,
“customfield_10022”: null,
“customfield_10023”: null,
“priority”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/priority/3”,
“iconUrl”: “https://crgsolutions10.atlassian.net/images/icons/priorities/medium.svg”,
“name”: “Medium”,
“id”: “3”
},
“labels”: ,
“customfield_10016”: null,
“customfield_10017”: null,
“customfield_10018”: {
“hasEpicLinkFieldDependency”: false,
“showField”: false,
“nonEditableReason”: {
“reason”: “PLUGIN_LICENSE_ERROR”,
“message”: “Portfolio for Jira must be licensed for the Parent Link to be available.”
}
},
“customfield_10019”: “0|hzzzzz:”,
“aggregatetimeoriginalestimate”: null,
“timeestimate”: null,
“versions”: ,
“issuelinks”: ,
“assignee”: null,
“updated”: “2019-07-11T11:09:57.523+0530”,
“status”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/status/10000”,
“description”: “”,
“iconUrl”: “https://crgsolutions10.atlassian.net/”,
“name”: “To Do”,
“id”: “10000”,
“statusCategory”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/statuscategory/2”,
“id”: 2,
“key”: “new”,
“colorName”: “blue-gray”,
“name”: “To Do”
}
},
“components”: ,
“timeoriginalestimate”: null,
“description”: null,
“customfield_10010”: null,
“customfield_10014”: null,
“customfield_10015”: null,
“timetracking”: {},
“customfield_10005”: null,
“customfield_10006”: null,
“security”: null,
“customfield_10007”: null,
“customfield_10008”: null,
“attachment”: ,
“customfield_10009”: null,
“aggregatetimeestimate”: null,
“summary”: “CRG Home Page has 402 error”,
“creator”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/user?accountId=5d25d0c97133f10c17e00a3f”,
“name”: “admin”,
“key”: “admin”,
“accountId”: “5d25d0c97133f10c17e00a3f”,
“emailAddress”: “juttiyavar1972@gmail.com”,
“avatarUrls”: {
“48x48”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=48&s=48”,
“24x24”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=24&s=24”,
“16x16”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=16&s=16”,
“32x32”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=32&s=32
},
“displayName”: “basu juttiyavar”,
“active”: true,
“timeZone”: “Asia/Calcutta”,
“accountType”: “atlassian”
},
“subtasks”: ,
“reporter”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/user?accountId=5d25d0c97133f10c17e00a3f”,
“name”: “admin”,
“key”: “admin”,
“accountId”: “5d25d0c97133f10c17e00a3f”,
“emailAddress”: “juttiyavar1972@gmail.com”,
“avatarUrls”: {
“48x48”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=48&s=48”,
“24x24”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=24&s=24”,
“16x16”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=16&s=16”,
“32x32”: “https://secure.gravatar.com/avatar/7a44d0ae81474a09291b676d0ea4010b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBJ-2.png&size=32&s=32
},
“displayName”: “basu juttiyavar”,
“active”: true,
“timeZone”: “Asia/Calcutta”,
“accountType”: “atlassian”
},
“aggregateprogress”: {
“progress”: 0,
“total”: 0
},
“customfield_10000”: “{}”,
“customfield_10001”: null,
“customfield_10002”: null,
“customfield_10003”: null,
“customfield_10004”: null,
“environment”: null,
“duedate”: null,
“progress”: {
“progress”: 0,
“total”: 0
},
“votes”: {
“self”: “https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-1/votes”,
“votes”: 0,
“hasVoted”: false
},
“comment”: {
“comments”: ,
“maxResults”: 0,
“total”: 0,
“startAt”: 0
},
“worklog”: {
“startAt”: 0,
“maxResults”: 20,
“total”: 0,
“worklogs”:
}
}
}

Any help is really appreciated!.

Hi @basujuttiyavar

This has been going on for a while now, so it would probably help if you were able to summarise for us what is and what isn’t working, giving the latest error message that you’re receiving. As far as I remember, you’re now able to successfully get data back from Postman, so your authorisation and request are correct and working. What have you then tried and what isn’t working?

Thanks Levy,

I am able to get the jira cloud data from postman.
But I am unable to get the jira cloud data from Java code and getting 405 error,

Code Snip:-

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/”);

        URL jiraREST_URL = new URL("https://crgsolutions10.atlassian.net/rest/api/3/issue/CRGWEB-1"); //assuming there's a CRGWEB-1 issue available to juttiyavar1972@gmail.com
        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("GET");
        conn.setRequestProperty("Authorization", "Basic " + Base64.encode("juttiyavar1972@gmail.com:<Token>".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());

        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","ABC-123"))
                            .add("summary", "sum")
                            //.add("description", "descr")
                            .add("issuetype", Json.createObjectBuilder().add("id", "10000"))
    ).build();

    return createIssue.toString();

}

}

Hi @basujuttiyavar

I think I saw a statement from Ian saying you shouldn’t pass in the encoded JSON body, but the line above looks like you are passing it in. Can you comment out this line and see if it makes any difference?

Thanks Levy,

Still i have 405 error even i comment the line.

Hi @basujuttiyavar

This line needs to be commented as well

Hi Levy,

I am unable to get the issues details in console…

Getting out as below:-

Looged in

Process finished with exit code 0

Can you please help me with the code snip to get data from Connection object.

Hello @basujuttiyavar, here’s a working Get issue code I made up from your base code. Just substitute your apiToken and it should work.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Base64;

public class RestTester {

    public static void main(String[] args) {
        try {

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

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

            conn.setRequestMethod("GET");
            
            conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("xxx@gmail.com:<apiToken>".getBytes("UTF-8")));
            System.out.println("Looged in");

            conn.setRequestProperty("Content-Type", "application/json");
            
    		if (conn.getResponseCode() != 200) {
    			throw new RuntimeException("Failed : HTTP error code : "
    					+ conn.getResponseCode());
    		}

    		BufferedReader br = new BufferedReader(new InputStreamReader(
    			(conn.getInputStream())));

    		String output;
    		System.out.println("Output from Server .... \n");
    		while ((output = br.readLine()) != null) {
    			System.out.println(output);
    		}

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

NOTE:
This is a GET request, once you’re successful here, it’s just a matter of passing the correct request body for your POST call.

Thanks iragido,

Sorry for so many too&Fro’s

I am not sure why i am getting Forbidden error as below.

Exception in thread “main” java.lang.RuntimeException: Failed : HTTP error code : 403
at RestTester.main(RestTester.java:28)

Please change https://crgsolutions2.atlassian.net to https://crgsolutions10.atlassian.net (change 2 to 10 since I think your instance with 2 is not available).

My Bad, Thanks a lot things are working :slight_smile:

1 Like

You’re welcome and glad to know you got it working now :slight_smile:
POST will be slightly different, just be sure to pass the proper request body. If you hit a wall and need help, kindly raise a new question here so we can close this case :wink:

Cheers,
Ian

Hi Iragudo,

it’s kind of your reading the webpage.
I have one question if I wanted to only to display “summary” or “description” in the output. I need to store the issue or say issue details in an object. can you help me out.

Hello @manjunathr,

Since your new question requires a different a solution from the original question, I suggest that you raise a separate post for this one so that if ever someone is facing the same scenario as this, the relevant solution/s can be easier to find.