iT邦幫忙

1

求關於 Google Apps Script 的 UrlFetchApp.fetch 的 options 設定如何加 ?

  • 分享至 

  • xImage

用 python 寫OK 要改寫成 Google Apps Script
在 node.js 要保留Session 與設定 Cookie 不熟 .... 看看那邊有錯

貼一下 python3 的 code(這是可登入的)

import requests,hashlib
session = requests.Session()
response= session.get("http://192.168.0.111:8888/login_en.html")
cookies = session.cookies.get_dict()
headers = {
    'Proxy-Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Origin': 'http://192.168.0.111:8888',
    'Upgrade-Insecure-Requests': '1',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Referer': 'http://192.168.0.111:8888/login_en.html',
    'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
}
for i in cookies:
    values = cookies[i]
str = "root:pass123:"+values
result = hashlib.md5(str.encode())
data = {
  'nonce': '',
  'encoded': "root:"+result.hexdigest(),
  'loginStatus': '-1'
}
response = session.post("http://192.168.0.111:8888/login_en.html", headers=headers, cookies=cookies, data=data, verify=False)
print(response.text)

改寫成 Google Apps Script 登入失敗 ... 求能人改寫

class SessionFetch {
  constructor() {
    this.cookie = "";
    this.his = [];
  }
  fetch(url, params = {}) {
    if (this.cookie != "") {
      if (!params.headers) {
        params.headers = { Cookie: this.cookie };
      } else if (!params.headers["Cookie"]) {
        params.headers["Cookie"] = this.cookie;
      }
    }
    this.his.push({ date: new Date(), url, params });
    const res = UrlFetchApp.fetch(url, params);
    this.cookie = res.getAllHeaders()["Set-Cookie"] || "";
    return res;
  }

  get history() {
    return this.his;
  }
}
function md5(inputString) {
  return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, inputString)
    .reduce((output, byte) => output + (byte < 0 ? byte + 256 : byte).toString(16).padStart(2, '0'), '');
}
function main() {
  var url="http://192.168.0.111:8888/login_en.html";
  const sf = new SessionFetch();
  const res1 = sf.fetch(url);
  var cookies = res1.getAllHeaders()['Set-Cookie']; 
  Logger.log(res1.getAllHeaders());  
  Logger.log(cookies);
  var values = cookies.substring(cookies.indexOf("=")+1,cookies.indexOf(";"));
  Logger.log(values);
  var str = "root:pass123:"+values;
  var md5_digestStr = md5(str);
  var info = "root:"+md5_digestStr
  Logger.log(info);
  var options = {
    'headers': {
      'Proxy-Connection': 'keep-alive',
      'Pragma': 'no-cache',
      'Cache-Control': 'no-cache',
      'Origin': 'http://192.168.0.111:8888',
      'Upgrade-Insecure-Requests': '1',
      'Content-Type': 'application/x-www-form-urlencoded',
      'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
      'Referer': 'http://192.168.0.111:8888/login_en.html',
      'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
      'Cookie': cookies,
    },
    'method': 'post',
    'payload': JSON.stringify({'nonce': '','encoded':info,'loginStatus': '-1'}),
    'muteHttpExceptions': true
  };
  const res2 = sf.fetch(url, options);
  Logger.log(res2.getContentText());
};
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
obarisk
iT邦研究生 1 級 ‧ 2023-03-20 19:45:36

跟 node.js 無關

沒有辦法用 google 的基礎建設造訪一個私有位置。

你可以看一下 error

https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

echochio iT邦高手 1 級 ‧ 2023-03-20 21:48:24 檢舉

是的我把 外網IP 換城內網IP
這IP 不能暴露 .......

我要發表回答

立即登入回答