无法通过Google脚本连接HP ALM API

时间:2019-01-26 17:21:17

标签: rest api google-apps-script google-sheets alm

我正在尝试调用HPQC API以获取一些报告,但是当我调用/ qcbin / rest / site-session时,我无法获得QCsession cookie。我已成功通过/ qcbin / authentication-point / authenticate进行了身份验证,但仍处于下一阶段。

并不是说我可以在没有Postman麻烦的情况下使用API​​,但是我的Google脚本不起作用。

下面是我正在打的电话:

var headers = { "Accept":"application/xml", 
"Content-Type":"application/xml", 
"method": "POST",
"headers" : {"Authorization": digestfull },
"muteHttpExceptions": true 
};


var resp = UrlFetchApp.fetch(url,headers);
var cookie = resp.getAllHeaders();//['Set-Cookie'];//.split(';')[0].toString(); 
Logger.log(cookie);
//Get session cookie
param = "/hp/rest/site-session";
var url2 = hpqc + param + api_key;
var payload = "<session-parameters><client-type>REST Client</client-type></session-parameters>";
var headers2 = { "Accept":"application/json", 
"Content-Type":"application/xml", 
"method": "POST",
"muteHttpExceptions" : true,
"cookie" : cookie,
"body" : payload
};

resp = UrlFetchApp.fetch(url2,headers2);

第一个电话工作正常,但第二个电话给我以下响应:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 401 Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /qcbin/rest/site-session. Reason:
<pre>    Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

似乎cookie没有正确发送。

您能帮我找到我的代码有什么问题吗?如果太明显了,我很抱歉,我正在学习使用Google脚本。

谢谢

1 个答案:

答案 0 :(得分:0)

我更改了以下代码

var payload = "<session-parameters><client-type>REST Client</client-type></session-parameters>";
var headers2 = { "Accept":"application/json", 
"Content-Type":"application/xml", 
"method": "POST",
"muteHttpExceptions" : true,
"cookie" : cookie,
"body" : payload
};

与此相辅相成

var payload = "<session-parameters><client-type>REST Client</client-type></session-parameters>";
var headers = {// "accept":"application/json", 
                  //"contentType":"application/json", 
                  "method": "POST",
                  "muteHttpExceptions" : true,
                  "headers" : {
                    "Content-type" : "application/xml",
                    "Accept" : "application/json",
                    "Cookie" : cookie
                  }, 
                  "payload" : payload
                 };

谢谢TheMaster给我的提示!