【发布时间】:2023-04-01 02:58:02
【问题描述】:
我正在尝试调用 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 script。
谢谢
【问题讨论】:
-
urlFetch.fetch() 的第二个参数/参数是选项参数而不是标题。查看文档。
-
这不会改变任何东西,它只是变量的名称。无论它被称为“选项”还是“标题”,它都按照文档中的方式构建。
标签: rest api google-apps-script google-sheets alm