Reputation: 195
Can anyone tell me how to use bit.ly (or any other url shortener) with a very long URL with a google sheets script.
The code I have is
function bitlyGroupId(accessToken) {
var headers = {'Authorization' : 'Bearer '+accessToken};
var params = {'headers' : headers};
var fetchUrl = 'https://api-ssl.bitly.com/v4/groups';
var response = UrlFetchApp.fetch(fetchUrl, params);
var group_guid = JSON.parse(response.getContentText()).groups[0].guid;
return group_guid
}
function bitlyShorten(longUrl) {
var accessToken = "<TOKEN>";
var group_guid = bitlyGroupId(accessToken);
var fetchUrl = 'https://api-ssl.bitly.com/v4/shorten';
var headers = {
'Authorization': 'Bearer '+ accessToken,
'Content-Type': 'application/json',
};
var payload = {
'group_guid' : group_guid,
'long_url' : longUrl
};
var params = {
'method' : 'post',
'headers' : headers,
'payload' : JSON.stringify(payload),
'muteHttpExceptions' : false
};
var response = UrlFetchApp.fetch(fetchUrl, params);
var shortUrl = JSON.parse(response.getContentText()).link;
return shortUrl;
}
This works well for shorter URLs but when I have a long URL (over 2k chars) i get the error INVALID_ARG_LONG_URL
.
Does anyone know how to get this working?
Upvotes: 0
Views: 320