首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java代码中JIRA REST的GET方法时的HTTP 401异常

使用Java代码中JIRA REST的GET方法时的HTTP 401异常
EN

Stack Overflow用户
提问于 2016-05-28 19:46:19
回答 1查看 1.5K关注 0票数 0

我正在尝试使用java代码来使用JIRA。它给了我401美元。此错误声明

代码语言:javascript
复制
HTTP Error 401 - Unauthorized: Access is denied due to invalid credentials.

但是我在代码中使用的链接和凭证在Firefox Client.It上运行得非常好,这让我发疯了。这是我的密码。

代码语言:javascript
复制
    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();

我得到以下异常

代码语言:javascript
复制
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)

花在这上面的几个小时都搞不清楚。

附注:另外,当我通过火狐RESTClient发送一个GET请求时,会出现下面的框,单击它5-7次使它消失,然后我就可以看到结果了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-03 21:41:10

我尝试了完全相同的代码(只做了一些很小的修改),它运行得很好。由此,我只能假设:

  1. 你提供的证件是不正确的
  2. 您的URL中有一个错误
  3. Base64.encodebase 64返回了一些错误信息。

试着使用

代码语言:javascript
复制
java.util.Base64.getEncoder().encode(cred.getBytes())

相反,

我的全部代码,供你参考:

代码语言:javascript
复制
    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();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37503187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档