Google Url Shortener with C# 19


Google DevelopersURL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. This is achieved by using a redirect on a domain name that is short, which links to the web page that has a long URL.    Google has a system for this called Google URL Shortener.   There is also an API for Google URL Shortener.    There is also a nuget package for working with it. Google.Apis.Urlshortener.v1 

A reader on the site just asked me if I could show you how to use the Google URL shortener with C#.

 

Prerequisite

  1. create a public API key.  If you dont know how read this. Google Developer Console Create Public API Key
  2. Create a new project in visual studio and install the following NuGet package. Install-Package Google.Apis.Urlshortener.v1

 

Connect to Google

The Google URL shortener API is a public API which means we don’t need to worry about authentication we can access it directly using a public API key.

 UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
            {
                ApiKey = "API KEY from Google developer console",
                ApplicationName = "Daimto URL shortener Sample",
            });

We now have a URLshortenerService we can use to access the api.

Shorten a URL

Shortening a url using Google Url Shortener is quite strait forward we call the insert method.  Which will create a new shortened URL for us.   The shortened URL can be found in the Id property.

 public static string shortenIt(string url)
        {
            UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
            {
                ApiKey = "API KEY from Google developer console",
                ApplicationName = "Daimto URL shortener Sample",
            });

            var m = new Google.Apis.Urlshortener.v1.Data.Url();
            m.LongUrl = url;
            return service.Url.Insert(m).Execute().Id;
        }

Unshorten a URL

If you have a shortened URL you can find its long URL by calling the Get method.  
public static string unShortenIt(string url)
        {
            UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
            {
                ApiKey = "API KEY from Google developer console",
                ApplicationName = "Daimto URL shortener Sample",
            });
            return service.Url.Get(url).Execute().LongUrl;
        }

The URL is in the LongURL property.

Putting it all together

Lets say we want to shorten the following url: https://www.youtube.com/watch?v=dQw4w9WgXcQ

var orignalURL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
var shortURL = DaimtoShort.shortenIt(orignalURL);
Console.WriteLine("My shortened URL: " + shortURL);

This outputs https://goo.gl/SsAhv which is a shortened version of our orignalURL.

Now lets say we have a shortened URL and want to find the original URL that it contained.

var newURL = DaimtoShort.unShortenIt(shortURL);
Console.WriteLine("My long URL: " + newURL);

This outputs https://www.youtube.com/watch?v=dQw4w9WgXcQ

Conclusion

The Google URL shortener API is a public API so we don’t need to be authenticated to use it. We can use the insert method to create a new shortened URL and the get method to get the long url for a shortened URL.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

19 thoughts on “Google Url Shortener with C#