How to Create Shortener URL in Google API using JavaScript

  1. <script type="text/javascript">  
  2.    function makeShort()  
  3.    {  
  4.       var longUrl=document.getElementById("longurl").value;  
  5.       var request = gapi.client.urlshortener.url.insert({  
  6.          'resource': {  
  7.             'longUrl': longUrl  
  8.             }  
  9.          });  
  10.          request.execute(function(response)  
  11.          {  
  12.             if(response.id != null)  
  13.             {  
  14.                str ="<b>Long URL:</b>"+longUrl+"<br>";  
  15.                str +="<b>Short URL:</b> <a href='"+response.id+"'>"+response.id+"</a><br>";  
  16.                document.getElementById("output").innerHTML = str;  
  17.             }  
  18.             else  
  19.             {  
  20.                alert("error: creating short url \n"+ response.error);  
  21.            }  
  22.          });  
  23.       }  
  24.       function getShortInfo()  
  25.       {  
  26.          var shortUrl=document.getElementById("shorturl").value;  
  27.          var request = gapi.client.urlshortener.url.get({  
  28.          'shortUrl': shortUrl,  
  29.          'projection':'FULL'  
  30.       });  
  31.       request.execute(function(response)  
  32.       {  
  33.          if(response.longUrl!= null)  
  34.          {  
  35.             str ="<b>Long URL:</b>"+response.longUrl+"<br>";  
  36.             str +="<b>Create On:</b>"+response.created+"<br>";  
  37.             str +="<b>Short URL Clicks:</b>"+response.analytics.allTime.shortUrlClicks+"<br>";  
  38.             str +="<b>Long URL Clicks:</b>"+response.analytics.allTime.longUrlClicks+"<br>";  
  39.             document.getElementById("output").innerHTML = str;  
  40.          }  
  41.          else  
  42.          {  
  43.             alert("error: "+response.error);  
  44.          }  
  45.       });  
  46.    }  
  47.    function load()  
  48.    {  
  49.       //Get your own Browser API Key from https://code.google.com/apis/console/  
  50.       gapi.client.setApiKey('your-api-key');  
  51.       gapi.client.load('urlshortener''v1',function(){document.getElementById("output").innerHTML="";});  
  52.    }  
  53.    window.onload = load;  
  54. </script>  
  55. <script src="https://apis.google.com/js/client.js"> </script>  
  56.    <body>  
  57.       <h1> Google URL Shortener API Demo </h1>  
  58.          <table>  
  59.             <tr>  
  60.                <td>  
  61.                   URL: <input type="text" id="longurl" name="url" size=30 value="" /> <br/>  
  62.                   <input type="button" value="Create Short" onClick="makeShort();" /> <br/> <br/>  
  63.                   URL: <input type="text" id="shorturl" name="url" value="" /> <br/>  
  64.                   <input type="button" value="Get Short URL Info" onClick="getShortInfo();" />  
  65.                </td>  
  66.             <td>  
  67.             <div id="output"></div>  
  68.          </td>  
  69.       </tr>  
  70. </table>