0

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

Headers needed Screenshot

Can anybody help me?

Thanks in advance.

ejooroo
  • 39
  • 4
  • When I accessed your showing URL, unfortunately, it seems that the status code 401 is returned. Can you confirm your URL again? And also, can you provide your expected output situation? – Tanaike Aug 18 '22 at 01:06
  • Hello @Tanaike. That URL is the API address, and it returns a JSON if Cookie and Token are incluyed in the header request. My problem is that this piece of code is the last part of the project, and before, there is needed to catch Cookie and Token in earlier requests. I can only get it to work if I manually copy and paste those values from the navigator dev console into const cookie = 'YYYYYYYYYYYYY'; const token = 'XXXXXXXXXX'; What I need is to catch those values from requests, and I don´t find out how to do it. – ejooroo Aug 18 '22 at 09:47
  • Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Aug 18 '22 at 11:22
  • English is not my native language either, so excuse me if I am not explaining myself clearly. I have edited the question hoping you can understand it better. The issue is that I need to obtain the cookie and token that the barchart.com website returns, to later use them in the API request. Thank you for your involvement. – ejooroo Aug 18 '22 at 13:51
  • Thank you for replying. Unfortunately, I cannot test your situation. In this case, unfortunately, I cannot propose an answer. This is due to my poor skill. I deeply apologize for my poor skill. – Tanaike Aug 19 '22 at 00:57

0 Answers0