我正在尝试使用Google脚本中的UrlFetchApp功能访问需要身份验证的文件,代码成功登录到网站,但使用收到的Cookie不允许访问更多页面。我怀疑我需要添加和/删除cookie中的一些元素,但不知道如何修改它。
浏览器在访问页面时将以下标题发送回服务器:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Cookie: userLang=en-GB; .ASPXAUTH=EA0D4BD7BB96D9152D865B70670B93ABB99EF2C1B8D75270D66AB467D5931C4BBE32F190AEF8BB0ADFB519D9C0F598033C2923451B1DE5A877F780C627AF5B27AB20DE2EC2AD71552B710BCB14BF47F6801AEADCDF2EA48BCC51ED05DB107B68DD1F17319CCB5DE03D27814FA142F6073527BDBC08BCD41056320F5E05A3B9A39B1E4E3A176B74F056DEA27DFFF74EA19C79E4BB4003644490D7337A519844BA8BF121CBEED197C584DB8AD4DA6122E6;
ASP.NET_SessionId=5b0fwgqbsjjbutmifmw2e1jc;
__RequestVerificationToken_L2FwcA2=3HFWt6okZixIdjZk2t0kzMp5vOhGbyHHnn3WekqAOM5sJe8OxzEfMOWyajXCb3SmpllSH88cioCLDYtM5y4fWsXkR5Y_G3DLggSAYduEDn81;
_ga=GA1.2.457212881.1522914125; _gid=GA1.2.870864118.1522914125
Host: standing-order.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0)
Gecko/20100101 Firefox/59.0
我从成功的Google脚本登录中收到的Cookie如下所示:
ASP.NET_SessionId=xw5ewo5c40nvxktozjblrbz4; path=/; HttpOnly,
.ASPXBrowserOverride=; expires=Wed, 04-Apr-2018 11:00:16 GMT; path=/;
HttpOnly,
.ASPXAUTH=23B38F90FA07516DAA254704615AD4FF41EAECFBB9686004257650E0DECA6EB9B04C6A4FB6A5B72A585456D6328EC43F5309A801E14EAC95977208888801A18769F13D3FE39BAD362223FBD588D7519D11BAD8B3C14E09B95F89EAD9115B842B664DC020AB3F3FDDBB44EF585F39F5DF0431BA23B0F06B6294FBE5A2EC19CECDB4635F4FA833E3EF5A594CC065FAB90FB0DE8C6652AFACC8CF3C9D91DB0F3E203AF0A05A605BB4F48807B7DF6F844A63;
path=/app/lbc; HttpOnly
我正在使用以下代码段进行身份验证:(上面的cookie是“sessionDetails”变量)
var sessionDetails = response.getAllHeaders()['Set-Cookie'];
UrlFetchApp.fetch("https://website.com/...",
{"headers" : {"Cookie" : sessionDetails},
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"Host": "standing-order.com",
"Upgrade-Insecure-Requests": "1"
});
有人可以说哪些元素可能导致身份验证问题,以及如何修改cookie以匹配?
我发现此帖子中的回复很有帮助,但它没有解决问题:Google Apps Script login to website with HTTP request
答案 0 :(得分:0)
我不知道这是否是执行此操作的“正确”方法,但是通过使用正则表达式提取我需要的部分Cookie,我已经能够在一些不同的网站上使用它匹配浏览器发送的cookie参数。显然,您可以忽略expires=
,path=
和HttpOnly
位。
function getCookieFromResponse(response) {
var sessionDetails = response.getAllHeaders()['Set-Cookie'];
if (!sessionDetails) {
throw "No cookies found";
};
var cookie = "userLang=en-GB;" // The site seems to want this, but it's not in the response headers. It might not be necessary
var searchResults = sessionDetails.match(/ASP\.NET_SessionId=[a-z0-9]+;/); //modify as needed to match the cookies your browser sends
if (searchResults) {
cookie = cookie + searchResults[0];
} else {
throw "Login failed";
}
var searchResults2 = sessionDetails.match(/\.ASPXAUTH=[A-Z0-9]+;/); //modify as needed to match the cookies your browser sends
if (searchResults2) {
cookie = cookie + searchResults2[0];
} else {
throw "Login failed";
}
return cookie;
}