Ich versuche, JIRA-API mit Java-Code zu konsumieren. Es gibt mir 401. Diesen Fehler besagt, dassHTTP 401-Ausnahme bei Verwendung der GET-Methode der JIRA-REST-API von Java Code
HTTP Error 401 - Unauthorized: Access is denied due to invalid credentials.
Aber die Verbindung und die Anmeldeinformationen, die ich im Code verwenden arbeiten völlig in Ordnung mit Firefox REST Client.It macht mich verrückt. Hier ist mein Code.
String url = "https://jira.atlassian.com/rest/api/1/search?jql=project=APS";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//Setting the Request Method header as GET
con.setRequestMethod("GET");
//Prepairing credentials
String cred= "[email protected]:spm12345678";
byte[] encoded = Base64.encodeBase64(cred.getBytes());
String credentials = new String(encoded);
//Setting the Authorization Header as 'Basic' with the given credentials
con.setRequestProperty ("Authorization", "Basic " + credentials);
//Setting the User-Agent header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
//reading the return
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream())
);
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String result = response.toString();
Ich erhalte die folgende Ausnahme
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: https://jira.atlassian.com/rest/api/2/issue/USER-274
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at jira_RESPAPI.getJSON(jira_RESPAPI.java:53)
at connector.main(connector.java:37)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://jira.atlassian.com/rest/api/2/issue/USER-274
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at jira_RESPAPI.getJSON(jira_RESPAPI.java:49)
Stunden dafür ausgegeben kippe es herausfinden.
p.s. Auch wenn ich eine GET-Anfrage über Firefox RESTClient sende, erscheint das folgende Feld, indem ich 5-7 Mal darauf klicke, es verschwindet, dann kann ich das Ergebnis sehen.
Hallo Somaiah, ich das Problem herausgefunden, wenn ich die API bin mit mit Versuchskonto akzeptiert keine Krediten. Aber sonst würde es. – Moon
Das ist seltsam. Mein Code war auch auf dem Testkonto und es funktionierte gut. Aber ich bin froh zu wissen, dass es funktioniert. –