用 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());
};
跟 node.js 無關
沒有辦法用 google 的基礎建設造訪一個私有位置。
你可以看一下 error
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app