Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HP ALM 12.21 REST API-401未经授权-C#_C#_Rest_Api_Alm_Unauthorized - Fatal编程技术网

HP ALM 12.21 REST API-401未经授权-C#

HP ALM 12.21 REST API-401未经授权-C#,c#,rest,api,alm,unauthorized,C#,Rest,Api,Alm,Unauthorized,我试图对我们的ALM 12.21服务器使用API,但结果总是“401未经授权”。似乎我正确地获取了auth cookie,但当我尝试在之后执行某项操作时,我是未经授权的 我使用此获取此来获取auth cookie(似乎有效): 401计划失败了 有人有ALM 12.21 REST API的完整工作代码吗?要使ALM REST API完美工作,您需要两个主要cookie LWSSO_COOKIE_密钥 QCSession almURL=”https://..com/qcbin/" authEndP

我试图对我们的ALM 12.21服务器使用API,但结果总是“401未经授权”。似乎我正确地获取了auth cookie,但当我尝试在之后执行某项操作时,我是未经授权的

我使用此获取此来获取auth cookie(似乎有效):

401计划失败了


有人有ALM 12.21 REST API的完整工作代码吗?

要使ALM REST API完美工作,您需要两个主要cookie

  • LWSSO_COOKIE_密钥
  • QCSession

    almURL=”https://..com/qcbin/"

    authEndPoint=almURL+“身份验证点/身份验证”

    qcSessionEndPoint=almURL+“rest/站点会话”

  • 成功响应
    authEndPoint
    后,您将获得LWSSO\u COOKIE\u密钥

    在下一次请求
    qcSessionEndPoint
    时使用该cookie,它将为您提供QCSession cookie

    在后续请求中使用LWSSO_COOKIE_密钥和QCSession COOKIE从ALM获取数据

    我看到您正在使用
    octet-stream
    来获得缺陷响应。当我检查文档时,它可以返回以下类型之一

    "application/xml"
    "application/atom+xml"
    "application/json"
    
    以防万一,如果您需要查看python中的一些工作实现,请看这里
    这可能会给你一些想法。

    谢谢你@Barney。您向正确的方向发送了我:-)对于任何感兴趣的人,我是这样管理的,例如,为了获得缺陷ID 473:

    登录以创建CookieContainer,然后使用它执行实际的ALM数据获取:

       private void button1_Click(object sender, EventArgs e)
        {
            string almURL = @"https://url/qcbin/";
            string domain = "domain";
            string project = "project";
            CookieContainer cookieContainer = LoginAlm2(almURL, "username", "password", domain, project);
    
            HttpWebRequest myWebRequest1 = (HttpWebRequest)WebRequest.Create(almURL + "/rest/domains/" + domain + "/projects/" + project + "/defects/473");
            myWebRequest1.CookieContainer = cookieContainer;
            myWebRequest1.Accept = "application/json";
            WebResponse webResponse1 = myWebRequest1.GetResponse();
            StreamReader reader = new StreamReader(webResponse1.GetResponseStream());
            string res = reader.ReadToEnd();
    
        }
    
    
       public CookieContainer LoginAlm2(string server, string user, string password, string domain, string project)
        {
            //Creating the WebRequest with the URL and encoded authentication
            string StrServerLogin = server + "/api/authentication/sign-in";
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(StrServerLogin);
            myWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Base64Encode(user + ":" + password);
            WebResponse webResponse = myWebRequest.GetResponse();
    
            CookieContainer c = new CookieContainer();
            Uri uri = new Uri(server);
    
            string StrCookie = webResponse.Headers.ToString();
            string StrCookie1 = StrCookie.Substring(StrCookie.IndexOf("LWSSO_COOKIE_KEY=") + 17);
            StrCookie1 = StrCookie1.Substring(0, StrCookie1.IndexOf(";"));
            c.Add(new Cookie("LWSSO_COOKIE_KEY", StrCookie1) { Domain = uri.Host });
    
            //Then the QCSession cookie
            string StrCookie2 = StrCookie.Substring(StrCookie.IndexOf("QCSession=") + 10);
            StrCookie2 = StrCookie2.Substring(0, StrCookie2.IndexOf(";"));
            c.Add(new Cookie("QCSession", StrCookie2) { Domain = uri.Host });
    
            //Then the ALM_USER cookie
            string StrCookie3 = StrCookie.Substring(StrCookie.IndexOf("ALM_USER=") + 9);
            StrCookie3 = StrCookie3.Substring(0, StrCookie3.IndexOf(";"));
            c.Add(new Cookie("ALM_USER", StrCookie3) { Domain = uri.Host });
    
            //And finally the XSRF-TOKEN cookie
            string StrCookie4 = StrCookie.Substring(StrCookie.IndexOf("XSRF-TOKEN=") + 12);
            StrCookie4 = StrCookie4.Substring(0, StrCookie4.IndexOf(";"));
            c.Add(new Cookie("XSRF-TOKEN", StrCookie4) { Domain = uri.Host });
    
            return c;
        }
    

    就像一个符咒一样工作:-)

    我认为您需要添加一个
    ,并将credentials
    值设置为trueThank。你有一些建议代码吗?一些测试。当你写think时很困难,而且不包括任何代码:)我知道如何用typescript,但我不确定用c#,它应该是你请求选项的一部分谢谢。我会努力的。一些文章认为12.21和12.50之间存在API端点差异。是这样吗?老实说,我还没有机会比较这两者。但是所有12.x都应该是一样的。
    "application/xml"
    "application/atom+xml"
    "application/json"
    
       private void button1_Click(object sender, EventArgs e)
        {
            string almURL = @"https://url/qcbin/";
            string domain = "domain";
            string project = "project";
            CookieContainer cookieContainer = LoginAlm2(almURL, "username", "password", domain, project);
    
            HttpWebRequest myWebRequest1 = (HttpWebRequest)WebRequest.Create(almURL + "/rest/domains/" + domain + "/projects/" + project + "/defects/473");
            myWebRequest1.CookieContainer = cookieContainer;
            myWebRequest1.Accept = "application/json";
            WebResponse webResponse1 = myWebRequest1.GetResponse();
            StreamReader reader = new StreamReader(webResponse1.GetResponseStream());
            string res = reader.ReadToEnd();
    
        }
    
    
       public CookieContainer LoginAlm2(string server, string user, string password, string domain, string project)
        {
            //Creating the WebRequest with the URL and encoded authentication
            string StrServerLogin = server + "/api/authentication/sign-in";
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(StrServerLogin);
            myWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Base64Encode(user + ":" + password);
            WebResponse webResponse = myWebRequest.GetResponse();
    
            CookieContainer c = new CookieContainer();
            Uri uri = new Uri(server);
    
            string StrCookie = webResponse.Headers.ToString();
            string StrCookie1 = StrCookie.Substring(StrCookie.IndexOf("LWSSO_COOKIE_KEY=") + 17);
            StrCookie1 = StrCookie1.Substring(0, StrCookie1.IndexOf(";"));
            c.Add(new Cookie("LWSSO_COOKIE_KEY", StrCookie1) { Domain = uri.Host });
    
            //Then the QCSession cookie
            string StrCookie2 = StrCookie.Substring(StrCookie.IndexOf("QCSession=") + 10);
            StrCookie2 = StrCookie2.Substring(0, StrCookie2.IndexOf(";"));
            c.Add(new Cookie("QCSession", StrCookie2) { Domain = uri.Host });
    
            //Then the ALM_USER cookie
            string StrCookie3 = StrCookie.Substring(StrCookie.IndexOf("ALM_USER=") + 9);
            StrCookie3 = StrCookie3.Substring(0, StrCookie3.IndexOf(";"));
            c.Add(new Cookie("ALM_USER", StrCookie3) { Domain = uri.Host });
    
            //And finally the XSRF-TOKEN cookie
            string StrCookie4 = StrCookie.Substring(StrCookie.IndexOf("XSRF-TOKEN=") + 12);
            StrCookie4 = StrCookie4.Substring(0, StrCookie4.IndexOf(";"));
            c.Add(new Cookie("XSRF-TOKEN", StrCookie4) { Domain = uri.Host });
    
            return c;
        }