iT邦幫忙

0

[Google App Script] UrlFetchApp沒有回應資料+詢問推薦的線上程式執行平台

  • 分享至 

  • xImage

問題是這樣的,我要向 https://www.gamer.com.tw/ajax/get_csrf_token.php 發送請求得到csrf token,我已經知道如果要正確得到token必須要在請求中夾帶一個叫BAHARUNE的cookie,我在用curl發請求時都有正確回應。

$ curl -v -X GET --cookie "BAHARUNE=a774f46115eb4c89f92b222b5df037e3680b987d680c7c0003f5837e6445b77a91f56908d22dd340386a" "https://www.gamer.com.tw/ajax/get_csrf_token.php"
Note: Unnecessary use of -X or --request, GET is already inferred.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 104.16.182.30:443...
* Connected to www.gamer.com.tw (104.16.182.30) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
*  CApath: none
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [122 bytes data]
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
{ [19 bytes data]
* TLSv1.3 (IN), TLS handshake, Certificate (11):
{ [2217 bytes data]
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
{ [79 bytes data]
* TLSv1.3 (IN), TLS handshake, Finished (20):
{ [52 bytes data]
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [52 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=CA; L=San Francisco; O=Cloudflare, Inc.; CN=sni.cloudflaressl.com
*  start date: Aug 14 00:00:00 2020 GMT
*  expire date: Aug 14 12:00:00 2021 GMT
*  subjectAltName: host "www.gamer.com.tw" matched cert's "*.gamer.com.tw"
*  issuer: C=US; O=Cloudflare, Inc.; CN=Cloudflare Inc ECC CA-3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0x1ff65c22980)
} [5 bytes data]
> GET /ajax/get_csrf_token.php HTTP/2
> Host: www.gamer.com.tw
> user-agent: curl/7.75.0
> accept: */*
> cookie: BAHARUNE=a774f46115eb4c89f92b222b5df037e3680b987d680c7c0003f5837e6445b77a91f56908d22dd340386a
>
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [230 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [230 bytes data]
* old SSL session ID is stale, removing
{ [5 bytes data]
* Connection state changed (MAX_CONCURRENT_STREAMS == 256)!
} [5 bytes data]
< HTTP/2 200
< date: Tue, 16 Mar 2021 04:41:41 GMT
< content-type: text/html; charset=UTF-8
< set-cookie: __cfduid=ddd29f2a40389d2b23da87079db9cc3f31615869701; expires=Thu, 15-Apr-21 04:41:41 GMT; path=/; domain=.gamer.com.tw; HttpOnly; SameSite=Lax; Secure
< vary: Accept-Encoding
< content-security-policy: frame-ancestors 'self' https://*.gamer.com.tw;
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
< cf-cache-status: DYNAMIC
< cf-request-id: 08daf004fb0000225491bb1000000001
< expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< server: cloudflare
< cf-ray: 630b4f819d452254-HKG
<
{ [56 bytes data]
100    56    0    56    0     0    380      0 --:--:-- --:--:-- --:--:--   38002496fe05f03d9a928333453cfa0ff6265857fda0190e6d160503705
* Connection #0 to host www.gamer.com.tw left intact

那個38002496fe05f03d9a928333453cfa0ff6265857fda0190e6d160503705就是token。
然而同樣的作法換到Google App Script(GAS)就得不到回應資料:
https://ithelp.ithome.com.tw/upload/images/20210316/20116468Wv35N3om2s.png

token.getContentText()得到的只有空字串,請問這到底是哪裡出問題了?

另外假如最後沒辦法修正GAS的這個問題的話,能不能推薦其他類似GAS這種具有可以自訂執行程式可長時間執行特性的平台?(Google Colaboratory不算,他很常自己斷線,有夠靠北)

b4106702 iT邦新手 5 級 ‧ 2021-04-02 01:44:54 檢舉
終於知道錯在哪了,cookie名稱沒有設
function signin() {
var token = UrlFetchApp.fetch('https://www.gamer.com.tw/ajax/get_csrf_token.php', {
'method': 'GET',
'headers': {
'Cookie': 'BAHARUNE=f133ddec8f58cedbc943cb8aaddddd7d1724c0e11725a464015810ca61bdf655808cad08ce4ade4dbe31'
}
});
Logger.log(token.getContentText());
Logger.log(token.getResponseCode());
}
這樣就有了
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答