Search code examples
javascriptjsonapigoogle-apps-scriptalpha-vantage

Google script for Alphavantage gives weird results


I am trying to make a google script which i want to use in Google sheets. I followed a tutorial video on youtube and i had the idea that my script was working well.

But then two strange things happen, and i can't figure out why.

Below is my code, this code is exactly doing what i want. It is showing the EPS of IBM in the google sheets and also in the log. However the moment i change the APIkey from "demo" to my own APIkey it is not working anymore. At that moment it is still showing the EPS in the LOG, but i will get an empty cell in Google Sheets.

I have no idea why that happens.

/**
 * Imports api data from alphavantage 
 * @customfunction
 */
function apiav(a) {
  var res = UrlFetchApp.fetch(
    'https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=demo'
  );
  var content = res.getContentText();
  var json = JSON.parse(content);
  var overviewvalue = json['EPS'];
  Logger.log(overviewvalue);
  return overviewvalue;
}

Solution

  • Try adding {validateHttpsCertificates: false} to your UrlFetchApp.fetch() to ignores any invalid certificates for HTTPS requests.

    Your code should look like this:

    function apiav(a) {
      var res = UrlFetchApp.fetch('https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=ABCDEFGH', {validateHttpsCertificates: false});
      var content = res.getContentText();
      var json = JSON.parse(content);
      var overviewvalue = json['EPS'];
      Logger.log(overviewvalue);
      return overviewvalue;
    }
    

    Output:

    enter image description here

    Reference:

    UrlFetchApp