Skip to content

Instantly share code, notes, and snippets.

@iamchriswick
Forked from hayageek/Google URL Shortener API
Last active August 29, 2015 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamchriswick/625316aea75619d9648f to your computer and use it in GitHub Desktop.
Save iamchriswick/625316aea75619d9648f to your computer and use it in GitHub Desktop.
<html>
<head>
</head>
<script type="text/javascript">
function makeShort()
{
var longUrl=document.getElementById("longurl").value;
var request = gapi.client.urlshortener.url.insert({
'resource': {
'longUrl': longUrl
}
});
request.execute(function(response)
{
if(response.id != null)
{
str ="<b>Long URL:</b>"+longUrl+"<br>";
str +="<b>Short URL:</b> <a href='"+response.id+"'>"+response.id+"</a><br>";
document.getElementById("output").innerHTML = str;
}
else
{
alert("error: creating short url");
}
});
}
function getShortInfo()
{
var shortUrl=document.getElementById("shorturl").value;
var request = gapi.client.urlshortener.url.get({
'shortUrl': shortUrl,
'projection':'FULL'
});
request.execute(function(response)
{
if(response.longUrl!= null)
{
str ="<b>Long URL:</b>"+response.longUrl+"<br>";
str +="<b>Create On:</b>"+response.created+"<br>";
document.getElementById("output").innerHTML = str;
}
else
{
alert("error: unable to get URL information");
}
});
}
function load()
{
gapi.client.setApiKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); //get your ownn Browser API KEY
gapi.client.load('urlshortener', 'v1',function(){});
}
window.onload = load;
</script>
<script src="https://apis.google.com/js/client.js"> </script>
<body>
URL: <input type="text" id="longurl" name="url" value="http://www.hayageek.com" /> <br/>
<input type="button" value="Create Short" onclick="makeShort();" /> <br/> <br/>
URL: <input type="text" id="shorturl" name="url" value="http://www.hayageek.com" /> <br/>
<input type="button" value="Get Short URL Info" onclick="getShortInfo();" />
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment