谷歌应用脚本中的 UrlFetchApp.fetch 删除 url 参数

UrlFetchApp.fetch in google app scripts delete url parameter

本文关键字:fetch 删除 url 参数 UrlFetchApp 应用 脚本 谷歌      更新时间:2023-09-26

我正在构建一个简单的电子表格,该电子表格使用 sharedcount.com API监视一系列URL的社交共享数据。

出于某种原因,UrlFetchApp.fetch() 消除了我作为要查询的端点参数传递的 url。

这是我的代码:

var sheet = SpreadsheetApp.getActiveSheet(),
          urls = sheet.getRange('A:A').getValues().slice(1),
          apikey = "XXXXXXXXXXXXXXXXX",
          apiEndPoint = "https://free.sharedcount.com/"
          data = [];
      urls.forEach(function(url, idx) {
        apiurl = apiEndPoint + '?url=' + encodeURIComponent(url) + '&apikey=' + apikey
        Logger.log(apiurl)
        sharedCountData = UrlFetchApp.fetch(apiurl)
        data[idx] = sharedCountData
      })

logger 函数返回正确构建的 url(例如:https://free.sharedcount.com/?url=http%3A%2F%2Fwww.teamvaxitalia.it%2F&apikey=XXXXXXXXXXXXXX),但运行我的代码时,当它到达 fetch() 时收到 401 错误,其中传递的端点是这样的:https://free.sharedcount.com/?url=&apikey=XXXXXXXXXXXXXX,错误是"不是有效的 URL"。缺少网址参数!

知道发生了什么吗?

谢谢

解决:出于某种原因,使用 UrlFetchApp.fetch 需要使用 https://free.sharedcount.com/url?url= 作为端点,而不仅仅是 https://free.sharedcount.com/?url=,即使在浏览器中两者都有效。

已解决:出于某种原因,使用UrlFetchApp.fetch需要使用 https://free.sharedcount.com/url?url=作为端点,而不仅仅是 https://free.sharedcount.com/?url=,即使在浏览器中两者都有效。

但是,欢迎就为什么会发生这种情况提出建议。