测试轨API抛出401未经授权的错误,即使具有正确的授权

时间:2019-12-25 07:53:22

标签: java api automation authorization testrail

我正在尝试建立与测试导轨的连接并传递正确的授权,但是会引发401 Unauthorized错误。

相同的代码可在不同的测试轨道上使用,但不适用于新实例。

我已在测试轨设置中启用了API集成,并生成了API授权密钥。 要解决此问题,我还有什么需要照顾的吗?

public static void SendHttpRequest(String jsonStr, String runId)

            String URL = prop.getProperty("TEST_RAIL_URL") + runId;  
            //TEST_RAIL_URL - https://testrail.xxxxxx.com/index.php?/api/v2/add_results_for_cases/
            java.net.URL urlObj = new java.net.URL(URL);

            HttpsURLConnection con = null;
            con = (HttpsURLConnection) urlObj.openConnection();
            con.setRequestMethod(prop.getProperty("API_METHOD"));
            //API_METHOD = POST
            con.setRequestProperty("Content-Type", prop.getProperty("API_CONTENT_TYPE"));
            //API_CONTENT_TYPE = application/json
            con.setRequestProperty("Authorization", prop.getProperty("API_AUTHORIZATION"));
            //API_AUTHORIZATION = Basic <base 64 encoded api key>

            System.out.println("Test Rail connection ready");
            System.out.println(con);
            con.setDoOutput(true);

            DataOutputStream outObj = new DataOutputStream(con.getOutputStream());
            outObj.writeBytes(jsonStr);
            outObj.flush();
            outObj.close();
            System.out.println("API request made to Test Rail");

控制台输出:

Test Rail connection ready
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://testrail.xxxxxx.com/index.php?/api/v2/add_results_for_cases/56
API request made to Test Rail
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt SendHttpRequest
INFO: 401Unauthorized
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt SendHttpRequest
INFO: {"error":"Authentication failed: invalid or missing user\/password or session cookie."}
401Unauthorized
{"error":"Authentication failed: invalid or missing user\/password or session cookie."}
Dec 24, 2019 9:37:17 PM com.testrail.integration.TestRailInt main
INFO: Enter finally. Closing

我猜想这与测试导轨设置(管理员访问权限)或网络级问题有关。

希望有人可以指出正确的方向。 TIA

1 个答案:

答案 0 :(得分:0)

您是否已检查以确保您的Testrail实例具有可用的API连接?

{{your_testrail_url}} index.php?/ admin / site_settings#

您将需要同时启用两个选项:启用API,启用API的会话身份验证。