使用来自 Java 代码的 JIRA REST API 的 GET 方法时出现 HTTP 401 异常
HTTP 401 exception while using GET method of JIRA REST API from Java Code
我正在尝试使用 java 代码使用 JIRA API。它给我 401。此错误指出
HTTP Error 401 - Unauthorized: Access is denied due to invalid credentials.
但是我在代码中使用的 link 和凭据在 Firefox REST Client.It 上运行得非常好,这让我发疯。这是我的代码。
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= "ahmed.tausif@spmconsulting.net: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();
我收到以下异常
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.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.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)
花了几个小时搞不懂。
p.s。此外,当我通过 Firefox RESTClient 发送 GET 请求时,会出现以下框,单击它 5-7 次使其消失,然后我可以看到结果。
我尝试了与您完全相同的代码(有一些非常小的改动)并且它运行良好。由此我只能假设:
- 您提供的信用不正确
- 您的 URL
有错别字
- Base64.encodeBase64 返回错误。
尝试使用
java.util.Base64.getEncoder().encode(cred.getBytes())
改为
我的全部代码,供大家参考:
package hello;
import org.springframework.http.HttpHeaders;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class Application {
public static void main(String args[]) throws IOException {
String url = "https://my-jira-domain.atlassian.net/rest/api/2/issue/MOON-13";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//Setting the Request Method header as GET
con.setRequestMethod("GET");
//Prepairing credentials
String cred = "my@jira.com:mypassword";
byte[] encoded = Base64.getEncoder().encode(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", HttpHeaders.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();
}
}
我正在尝试使用 java 代码使用 JIRA API。它给我 401。此错误指出
HTTP Error 401 - Unauthorized: Access is denied due to invalid credentials.
但是我在代码中使用的 link 和凭据在 Firefox REST Client.It 上运行得非常好,这让我发疯。这是我的代码。
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= "ahmed.tausif@spmconsulting.net: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();
我收到以下异常
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.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.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)
花了几个小时搞不懂。
p.s。此外,当我通过 Firefox RESTClient 发送 GET 请求时,会出现以下框,单击它 5-7 次使其消失,然后我可以看到结果。
我尝试了与您完全相同的代码(有一些非常小的改动)并且它运行良好。由此我只能假设:
- 您提供的信用不正确
- 您的 URL 有错别字
- Base64.encodeBase64 返回错误。
尝试使用
java.util.Base64.getEncoder().encode(cred.getBytes())
改为
我的全部代码,供大家参考:
package hello;
import org.springframework.http.HttpHeaders;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class Application {
public static void main(String args[]) throws IOException {
String url = "https://my-jira-domain.atlassian.net/rest/api/2/issue/MOON-13";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//Setting the Request Method header as GET
con.setRequestMethod("GET");
//Prepairing credentials
String cred = "my@jira.com:mypassword";
byte[] encoded = Base64.getEncoder().encode(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", HttpHeaders.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();
}
}