Tried to custom the shorten linked (bit.ly) via api but it keep return me 406

1.1k Views Asked by At

As stated, I tried to customize the link (back-half). It keep return me 406. Or should I use Post/custom_bitlinks instead?

Here is my current code:

function bitlyori (i, title){
var form = {
    "group_guid": "MINE",  
    "domain": "bit.ly",  
    "long_url": i,
    "title" : title
    }; 
const MY_TOKEN = "MINE";
const option = {
  headers: { Authorization: `Bearer ${MY_TOKEN}` },
  method: 'post',
  contentType: 'application/json',
  payload: JSON.stringify(form),
  };
var result = UrlFetchApp.fetch('https://api-ssl.bitly.com/v4/bitlinks', option);
return (JSON.parse(result.getContentText()));
}

function bitly(url,title,custom) {
var temp = bitlyori(url, title);
var form_2 = { 
  "custom_bitlinks": [custom] ,
    };
const MY_TOKEN = "MINE";
const option_2 = {
  headers: { Authorization: `Bearer ${MY_TOKEN}` },
  method: 'patch',
  payload: form_2}; 
var temp_link = 'https://api-ssl.bitly.com/v4/bitlinks/'+ temp["id"];
var result_2 = UrlFetchApp.fetch(temp_link, option_2);
return (JSON.parse(result_2.getContentText()));
}

P.S. here is the reference: https://dev.bitly.com/api-reference#updateCustomBitlink


UPDATE: Somehow made it through the success one; yet wasn't able to patch anything to the original. Here is the part that I fixed it.

function bitly(url,title2,custom) {
var temp = bitlyori(url, title2);
var form_2 = { 
  "custom_bitlinks": [custom]
  //"bitlink_id": temp["id"]
    };
const MY_TOKEN = "MINE";
const option_2 = {
  headers: { Authorization: `Bearer ${MY_TOKEN}`},
  method: 'patch',
  contentType: 'application/json',
  payload: JSON.stringify(form_2),
  muteHttpExceptions: true
  }; 
var temp_link = 'https://api-ssl.bitly.com/v4/bitlinks/'+temp["id"];
//return (temp_link);
var result_2 = UrlFetchApp.fetch(temp_link, option_2);
var CusAr = result_2.getContentText();
return (CusAr);}
0

There are 0 best solutions below