我正在尝试建立与测试轨的连接并通过正确的授权,但它抛出 401 未经授权的错误。
相同的代码在不同的测试轨道上工作,但不适用于新实例。
我已经在测试轨道设置中启用了 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
我猜这与测试轨道设置(管理员访问权限(或网络级别问题有关。
希望有人能为我指出正确的方向。 蒂亚
您是否检查过以确保您的 Testrail 实例具有可用的 API 连接?
{{your_testrail_url}}index.php?/admin/site_settings#
您需要启用两个选项:启用 API、启用 API 的会话身份验证。
在 Base 64 字符串中添加身份验证标头对我有用
string auth = Convert.ToBase64String(
Encoding.ASCII.GetBytes($"{UserId}:{UserPasswrd}"));
request.AddHeader("Authorization", "Basic " + auth);