Google脚本UrlFetchApp授权失败,如何修改Cookie? [英] Google Scripts UrlFetchApp authorisation failing, how do I modify the cookie?

查看:73
本文介绍了Google脚本UrlFetchApp授权失败,如何修改Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google脚本中的UrlFetchApp函数访问需要身份验证的文件,该代码已成功登录到该站点,但是使用接收到的cookie不允许访问其他页面.我怀疑我需要添加和/或删除Cookie中的某些元素,但不知道如何修改它.

I'm trying to access a file that requires authentication using the UrlFetchApp function in google scripts, the code successfully logs onto the site, but using the received cookie doesn't allow access to further pages. I suspect I need to add and/remove some elements in the cookie but don't know how to modify it.

浏览器在访问页面时将以下标头发送回服务器:

The browser sends the following headers back to the server when it accesses the page:

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如下:


The Cookie that I receive from a successful google scripts login looks like:

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"变量)

I'm using the following snippet of code for authentication: (the above cookie is the "sessionDetails" variable)

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以使其匹配?

Can anyone say which elements might be causing the problems with authentication, and how to modify the cookie to match?

我发现此线程中的响应很有帮助,但尚未解决问题:

I have found the responses in this thread helpful, but it hasn't solved the problem: Google Apps Script login to website with HTTP request

推荐答案

我不知道这是否是正确"的方法,但是我已经能够在一些其他网站上使用它通过使用正则表达式提取我需要的Cookie部分,以匹配浏览器发送的Cookie参数.显然,您可以省略expires=path=HttpOnly位.

I don't know if this is the "right" way to do this, but I've been able to get this to work on a few different websites by using regex to extract the part of the cookie I need in order to match the cookie parameters sent by the browser. Apparently, you can leave out the expires=, path=, and HttpOnly bits.

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;
}

这篇关于Google脚本UrlFetchApp授权失败,如何修改Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆