Google Url Shortener with C# 9


Please Share

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.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.


Leave a comment

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

9 thoughts on “Google Url Shortener with C#

  • Elena

    I just like the helpful info you supply to your articles.
    I will bookmark your blog and take a look at once more right here regularly.
    I am slightly certain I will be informed many new stuff proper right here!
    Good luck for the next!

  • Kereta sewa kuala lumpur

    Hola! I’ve been following your site for a while now and finally got
    the courage to go ahead and give you a shout out from Austin Texas!
    Just wanted to tell you keep up the great job!

  • Vu The Phong

    hi, love your post. But how can i get analytics for shorturlclick i try this:

    UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
    {
    ApiKey = “AIzaSyBpTNXR73kM48_Y9vZRL3QDK8gROA9IKdA”,
    });

    return service.Url.Get(url).Execute().Analytics.AllTime.ShortUrlClicks.Value.ToString();

    but its not work. Can you help me?

  • Vikash Kumar

    This doesn’t work for me. I am trying to use a textbox to enter a url and then shorten. But i get nothing
    protected void Button2_Click(object sender, EventArgs e)
    {
    string url = TextBox2.Text;
    var shortURL = shortenIt(url);
    Console.WriteLine(“My shortened URL: ” + shortURL);
    }
    public static string shortenIt(string url)
    {
    UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
    {
    ApiKey = “MyApiKey”,
    ApplicationName = “URL Shortener API”,
    });

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

    }
    }

    Also, do i need to change something in the web.config file?
    and it’s a local host
    please help