从Google Apps脚本调用Bitly API时出现错误:INVALID_ARG_LONG_URL

huangapple go评论59阅读模式
英文:

Error when calling bitly api from Google apps script: INVALID_ARG_LONG_URL

问题

在使用 Google Apps Script 通过 Bit.ly API 缩短 URL 时,我的 URL 看起来没问题,但 Bitly 拒绝并表示我的 URL 太长。以下是你提供的代码:

function shortenURL() {
  var longUrl = 'www.google.com';
  var shortURL;
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify({
      "long_url": longUrl,
    }),
    'muteHttpExceptions': false,
    'headers': {'Authorization': 'Bearer ' + 'xxx (我的授权密钥)'};
  Logger.log(longUrl);
  shortURL = UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten", options).getContentText();
  Logger.log(shortURL)
}

Bitly 拒绝并返回错误代码 400,错误消息为 "INVALID_ARG_LONG_URL",表示提供的值无效。你可能需要检查 URL 是否符合 Bitly 的要求,以确保它不会被拒绝。如果 URL 太长,你可以尝试缩短它或考虑其他方式来处理。

希望这些建议对你有帮助。

英文:

Trying to shorten URLs through a Google apps script using Bit.ly api:

function shortenURL() {
  var longUrl = 'www.google.com';
  var shortURL;
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify({
      "long_url": longUrl,
    }),
    'muteHttpExceptions': false,
    'headers': {'Authorization': 'Bearer ' + 'xxx (my authorization key)'};
  Logger.log(longUrl);
  shortURL = UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten", options).getContentText(); // Modified
  Logger.log(shortURL)
}

My URL looks fine but Bitly is rejection saying my URL is too long:

10:59:40 AM	Notice	Execution started
10:59:40 AM	Info	www.google.com
10:59:40 AM	Error	
Exception: Request failed for https://api-ssl.bitly.com returned code 400. Truncated server response: {"message":"INVALID_ARG_LONG_URL","resource":"bitlinks","description":"The value provided is invalid.","errors":[{"field":"long_url","error_code":"... (use muteHttpExceptions option to examine full response)
shortenURL	@ BitlyTest.gs:14

Any suggestions appreciated.

答案1

得分: 1

修改点:

  • 在你的脚本中,options 没有被 {} 包围。请注意这一点。
  • 我认为 var longUrl = 'www.google.com'; 可能应该是 var longUrl = 'https://www.google.com';。我认为这可能是你当前问题的原因。

当这些点反映在你的脚本中时,它变成如下所示。

修改后的脚本:

function shortenURL() {
  var longUrl = 'https://www.google.com';
  var shortURL;
  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify({
      "long_url": longUrl,
    }),
    'muteHttpExceptions': false,
    'headers': { 'Authorization': 'Bearer ' + 'xxx (我的授权密钥)' }
  };
  Logger.log(longUrl);
  shortURL = UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten", options).getContentText(); // 修改
  Logger.log(shortURL)
}
  • 当我使用我的访问令牌测试了这个修改后的脚本时,我确认可以得到正确的响应值,没有错误。
英文:

Modification points:

  • In your script, options is not enclosed by }. Please be careful about this.
  • I thought that var longUrl = 'www.google.com'; might be var longUrl = 'https://www.google.com';. I thought that this might be the reason for your current issue.

When these points are reflected in your script, it becomes as follows.

Modified script:

function shortenURL() {
  var longUrl = 'https://www.google.com';
  var shortURL;
  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify({
      "long_url": longUrl,
    }),
    'muteHttpExceptions': false,
    'headers': { 'Authorization': 'Bearer ' + 'xxx (my authorization key)' }
  };
  Logger.log(longUrl);
  shortURL = UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten", options).getContentText(); // Modified
  Logger.log(shortURL)
}
  • When I tested this modified script using my access token, I confirmed that the correct response value could be obtained with no errors.

huangapple
  • 本文由 发表于 2023年2月14日 03:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定