【问题标题】:Test rail API throwing 401 Unauthorized error even with correct authorization即使授权正确,测试 Rail API 也会抛出 401 Unauthorized 错误
【发布时间】:2020-04-15 23:34:41
【问题描述】:

我正在尝试建立与测试轨道的连接并通过正确的授权,但它会引发 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

【问题讨论】:

    标签: java api automation authorization testrail


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,我启用了它。无论如何,我能够解决这个问题。问题在于身份验证密钥的 base 64 编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2017-03-16
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多