Skip to content

Instantly share code, notes, and snippets.

@misterhay
Last active May 12, 2023 20:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterhay/38a500545ce7abc75b875f33f01c9f51 to your computer and use it in GitHub Desktop.
Save misterhay/38a500545ce7abc75b875f33f01c9f51 to your computer and use it in GitHub Desktop.
JavaScript code to interact with the Bitly API
function bitlyGroupId() {
var accessToken = ''; // paste in your access token from https://bitly.is/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;
Logger.log(group_guid)
return group_guid
}
function bitlyShorten(longUrl) {
var accessToken = ''; // paste in your access token from https://bitly.is/accesstoken
var group_guid = bitlyGroupId(); // or you can run the function and paste in the group_guid here
var fetchUrl = 'https://api-ssl.bitly.com/v4/shorten';
var headers = {
'Authorization': 'Bearer '+ accessToken,
'Content-Type': 'application/json',
};
var payload = {
//'domain' : 'bit.ly',
//'title' : '',
//'tags' : ['', ''],
'group_guid' : group_guid,
'long_url' : longUrl
};
var params = {
'method' : 'post',
'headers' : headers,
'payload' : JSON.stringify(payload),
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch(fetchUrl, params);
var shortUrl = JSON.parse(response.getContentText()).link;
Logger.log(shortUrl);
return shortUrl;
}
function bitlyExpand(bitlink_id) {
// bitlink_id should not be in the form bit.ly/xxxxxx
// or something like var bitlink_id = bitly_url.substring(bitly_url.indexOf('//')+2);
var accessToken = ''; // paste in your access token from https://bitly.is/accesstoken
var fetchUrl = 'https://api-ssl.bitly.com/v4/expand'
var headers = {
'Authorization': 'Bearer '+ accessToken,
'Content-Type': 'application/json',
};
var payload = {
'bitlink_id' : bitlink_id
};
var params = {
'method' : 'post',
'headers' : headers,
'payload' : JSON.stringify(payload),
};
var response = UrlFetchApp.fetch(fetchUrl, params);
var longUrl = JSON.parse(response.getContentText()).long_url;
return longUrl;
}
function bitlyStats(bitlink_id) {
// bitlink_id should not be in the form bit.ly/xxxxxx
// or something lik var bitlink_id = bitly_url.substring(8);
var accessToken = '';
var fetchUrl = 'https://api-ssl.bitly.com/v4/bitlinks/' + bitlink_id + '/clicks/summary';
var headers = {
'Authorization': 'Bearer '+ accessToken,
'Content-Type': 'application/json',
};
var params = {
'method' : 'get',
'headers' : headers,
};
var response = UrlFetchApp.fetch(fetchUrl, params);
var clickCount = JSON.parse(response.getContentText()).total_clicks;
return clickCount;
}
@misterhay
Copy link
Author

Receiving Error : Exception: Request failed for https://api-ssl.bitly.com returned code 404. Truncated server response: 404 page not found
(use muteHttpExceptions option to examine full response)

Did you paste in the access token from https://bitly.is/accesstoken ?

Yes

Hmm, not sure. Check out https://dev.bitly.com

@Dputz
Copy link

Dputz commented Apr 22, 2022

Receiving Error : Exception: Request failed for https://api-ssl.bitly.com returned code 404. Truncated server response: 404 page not found
(use muteHttpExceptions option to examine full response)

Did you paste in the access token from https://bitly.is/accesstoken ?

Yes

Hmm, not sure. Check out https://dev.bitly.com

Here is the error
Exception: Request failed for https://api-ssl.bitly.com returned code 404. Truncated server response: 404 page not found
(use muteHttpExceptions option to examine full response)
bitlyStats @ github -gist.gs:74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment