I want to get data from barchart.com using Google Apps Script, The issue is I need to find cookie and token to pass them in the request header to the API url.
This solution works fine in python, but i'm noob and tried all solutions available in StackOverflow and I keep getting no results.
Scraping a table on Barchart website using python
Apparently in python
s=requests.Session()
r=s.get(url)
works differently than Gogle Apps Script
response = UrlFetchApp.fetch(url);
I'm stuck with this issue.
My piece of code works if I copy and paste manually both cookie and token from the Chrome Dev conslole:
function myFunction() {
//STEP 1
// First of all, (the reason for my question)
// Here is where I Need help to get cookie and token to use in the next request
// I don´t know what URL fetch to find them
const cookie = '????????????';
const token = '?????????????';
//
//_____________________________________________________
// STEP 2 (resolved)
// Once I have Cokkie an Token from step 1, I pass them in the header of the query to the API address that returns the data in json format
var urlAPI = 'https://www.barchart.com/proxies/core-api/v1/historical/get?symbol=%24MMTH&fields=tradeTime.format(m%2Fd%2FY)%2CopenPrice%2ChighPrice%2ClowPrice%2ClastPrice%2CpriceChange%2CpercentChange%2Cvolume%2CsymbolCode%2CsymbolType&type=eod&orderBy=tradeTime&orderDir=desc&limit=65&meta=field.shortName%2Cfield.type%2Cfield.description&raw=1';
var map = {
"x-xsrf-token": token,
"cookie": cookie
}
var response = UrlFetchApp.fetch(urlAPI, { headers: map});
Logger.log(response);
var json = JSON.parse(response);
Logger.log(json.data[0]);
}
Can anybody help me?
Thanks in advance.