Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[GAS] create short url (google, bitly)
// Google URL Shortener
// https://developers.google.com/url-shortener/v1/getting_started
function createGoogleShortUrl(url) {
var result = UrlShortener.Url.insert({
longUrl: url
});
return result.id;
}
// Bitly
// https://dev.bitly.com/links.html#v3_shorten
const OAUTH_TOKEN = '[OAUTH_TOKEN]';
function createBitlyUrl(url) {
var endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + OAUTH_TOKEN + '&longUrl=' + url;
var result = UrlFetchApp.fetch(endpoint, {//
method: "GET",
contentType: "application/json;"
});
var json = JSON.parse(result.getContentText('utf-8'));
return json.data.url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment