Skip to content

Instantly share code, notes, and snippets.

@oksuz
Last active August 12, 2020 12:13
Show Gist options
  • Save oksuz/59592679072d503791ee86a284672f85 to your computer and use it in GitHub Desktop.
Save oksuz/59592679072d503791ee86a284672f85 to your computer and use it in GitHub Desktop.
Console tool for shortening urls through bitly

Installation

first thing first you should get access token from bitly with following instructions:

https://support.bitly.com/hc/en-us/articles/230647907-How-do-I-generate-an-OAuth-access-token-for-the-Bitly-API-

run following command for installation and refresh your bash session

curl -XGET -o /usr/local/bin/url-shortener https://gist.githubusercontent.com/oksuz/59592679072d503791ee86a284672f85/raw/a2c4f824f56b125819f66fd65413ee2205b0454f/bitly-console.sh && chmod +x /usr/local/bin/url-shortener

then use like url-shortener [url]

#!/bin/bash
# follow instructions for creating your bitly access token
# https://support.bitly.com/hc/en-us/articles/230647907-How-do-I-generate-an-OAuth-access-token-for-the-Bitly-API-
FILE="${HOME}/.bitly"
if [ -z "$1" ]; then
echo "usage: shorten [url]"
exit 1
fi
if [ ! -f "$FILE" ]; then
echo -n "Enter your bitly access token [ENTER]: "
read TOKEN
if [ -z "${TOKEN}" ]; then
echo "token was entered empty"
exit 1
else
echo "${TOKEN}" > "${FILE}"
fi
fi
TOKEN=`cat ${FILE}`
URL="https://api-ssl.bitly.com/v3/shorten?access_token=$TOKEN&format=txt&longUrl=$1"
curl -XGET "$URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment