Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Google URL shortener API with Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 6.74k
    Comment on it

    Hello Readers,

    goo.gl  service at Google URL shortener API takes long URLs and squeezes them into less characters to make a small link so that can be easily share on social media and email to friends. This API is free with a daily limit of requests i.e ( 1,000,000 requests daily). To access Google API we need an API KEY.

     

    To create API KEY click here: Google Developer Console.

     

    URL shortener API is available in JavaScript, Java, Python, Ruby, PHP, .NET and Objective-C. In this post we will use JavaScript API.

     

    Here is the full example:

    <html>
        <head>
        </head>
        <script type="text/javascript">
            var longUrl = 'http://www.google.com/';
            function makeShort() 
            {
                var request = gapi.client.urlshortener.url.insert({
                    'resource': {
                        'longUrl': longUrl // Your long URL
                    }
                });
                request.execute(function(response) 
                {
                    console.log("Short URL:"+ response.id);
                });
            }
             
            function googleLoad()
            {
                // Insert your browser API key generated from Google Developer Console
                gapi.client.setApiKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); 
    
                gapi.client.load('urlshortener', 'v1',function(){});
             
            }
            window.onload = googleLoad;     
        </script>
        <script src="https://apis.google.com/js/client.js"> </script>
     
        <body>
            <button onclick="makeShort();">Create short URL</button> 
        </body>
    </html>

     

    In the above example, I am using Google library client.js. We need to set our API key generated from Google developer console then call the API. urlshortener is API name and v1 is version.

     

    For more details visit: URL shortener

     

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: