代码之家  ›  专栏  ›  技术社区  ›  xd1936

在Google Apps Script 500中获取NWS API时出错

  •  1
  • xd1936  · 技术社区  · 7 年前

    我正在尝试从中获取NWS警报 their new GeoJSON API ,但在通过谷歌应用程序脚本获取数据时,出现了500个错误。

    function getAlerts() {
      var rawData = UrlFetchApp.fetch('https://api.weather.gov/alerts/active', {muteHttpExceptions: true});
      Logger.log(rawData);
    }
    

    日志:

    {
      "correlationId": "085cf198-e7ee-427c-8bb4-9bfd9f91fe9f",
      "title": "Unexpected Problem",
      "type": "https://api.weather.gov/problems/UnexpectedProblem",
      "status": 500,
      "detail": "An unexpected problem has occurred.",
      "instance": "https://api.weather.gov/requests/085cf198-e7ee-427c-8bb4-9bfd9f91fe9f"
    }
    

    如果我从web浏览器转到此URL,或 another service ,工作正常。帮助

    1 回复  |  直到 7 年前
        1
  •  2
  •   Costantin    6 年前

    标题有问题,您可以在 documentation (在API参考下)您应该为每个端点使用哪个标头,然后您只需像这样添加它们:

      var headers = {Accept: "application/ld+json"}
      var options = {"headers": headers};
      var currentWeatherUrl = "https://api.weather.gov/alerts/active?active=1"
      var response = UrlFetchApp.fetch(currentWeatherUrl, options);