Bit.ly gives 404 from Google Apps Script

480 Views Asked by At

I am trying to shorten URLs from Google Apps Script, but I keep getting 404 errors and I don't know why. Please help.

function shortenUrl(longUrl){
  var options = {
    'method' : 'post',
    'contentType': 'application/json',  
    'payload' : JSON.stringify({
      "long_url": longUrl,
    }),
    //  'muteHttpExceptions': true,
    'headers': {'Authorization': 'Bearer ' + BITLY_TOKEN,
//                'Host': 'https://api-ssl.bitly.com',
                'Content-Type': 'application/json'}
  };

  return UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten HTTP/1.1", options).getContentText();
}
1

There are 1 best solutions below

4
Tanaike On

How about this modification?

Modified script:

function shortenUrl(longUrl){
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify({
      "long_url": longUrl,
    }),
    //  'muteHttpExceptions': true,
    'headers': {'Authorization': 'Bearer ' + BITLY_TOKEN} // Modified
  };

  return UrlFetchApp.fetch("https://api-ssl.bitly.com/v4/shorten", options).getContentText(); // Modified
}
  • For above script, please confirm whether BITLY_TOKEN is declared, again.

References: