【问题标题】:Error from UrlFetchApp when passing Short URL传递短 URL 时 UrlFetchApp 出错
【发布时间】:2018-10-12 06:07:12
【问题描述】:

使用UrlFetchApp.fetch 调用获取请求时遇到以下异常。

异常:https://shorturl.com (myshorturl) 的请求失败 返回代码 302

longurl 长度为 5606 长度时,我没有收到错误消息。 当 longurl 长度超过 5609 长度时出现错误。

 var url = shortenUrl(longurl);
 Logger.log("URL:" + url);
 var response = UrlFetchApp.fetch(url, {
     "method": "get",followRedirects: true
 });
 Logger.log("Response" + response.getContentText())


function shortenUrl(longURL) {

var url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=mykey";

var payload = {
    "dynamicLinkInfo": {
        "dynamicLinkDomain": "mypage.page.link",
        "link": longURL,
        "iosInfo": {
            "iosBundleId": "mybundleId"
        }
    }
};

var parameters = {
    method: 'post',
    payload: JSON.stringify(payload),
    contentType: 'application/json',
    muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, parameters);
var myArr = JSON.parse(response);
var shortLink = myArr['shortLink'];
return shortLink;
}

【问题讨论】:

  • 也添加 muteHTTP。显示完整回复
  • muteHttpExceptions : true 用于静音异常。我也尝试在我的代码中添加它,但我没有成功。我最近发现确切的问题如下,当 longurl 长度为 5606 长度时,我没有收到错误。当 longurl 长度超过 5609 长度时出现错误。
  • 这将超过 2kB/调用硬限制。

标签: google-apps-script http-status-code-302 firebase-dynamic-links urlfetch google-url-shortener


【解决方案1】:

shortUrl 将重定向到另一个 url。 302 Found 状态码通知客户端存在重定向并提供重定向 url。尝试另外设置以下参数:

followRedirects: true,    
muteHttpExceptions: true

编辑:
根据新信息,longUrl 长度超过 5000 个字符,请注意 UrlFetch 调用对 url 长度有硬性限制,目前为 2kB/调用。如果我们假设每个 url 字符 1 个字节,则广告可接受的最大 url 长度约为 2048 个字符。

参考资料:

【讨论】:

  • 我 followRedirects 默认为 true,我也尝试添加 followRedirects: true 但仍然面临同样的问题
猜你喜欢
  • 1970-01-01
  • 2022-09-30
  • 2016-11-03
  • 2023-03-28
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多