URL 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
- create a public API key. If you dont know how read this. Google Developer Console Create Public API Key
- 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.
+1 if you where Rickrolled 🙂
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!
Hola! I’ve been reading your weblog for a while now and finally got the courage
to go ahead and give you a shout out from Huffman Tx! Just
wanted to tell you keep up the excellent job!
Now thats the kind of comment i like 🙂
Thanks!
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!
BaseClientService was not found when shortner package downloaded from nuget.
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?
try Url.List Note: its going to require Oauth2 authentication of the user who created the link in question you cant use an API key to get that data back its private.
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
Nice, tnks
This website was… how doo I say it? Relevant!!
Finally I’ve found something which helped me. Thanks!
Thanks.
Very Good
Thank you Linda. Helped me a lot and so easy
Hello, the whole thing is going perfectly here and ofcourse
every one is sharing information, that’s in fact excellent, keep up
writing.
I’m still learning from you, while I’m trying to reach my goals.
I certainly love reading all that is posted on your site.Keep the stories coming.
I liked it!
It does not work!
JsonReaderException: Error parsing NaN value. Path ”, line 1, position 1
This is the error i get on the line:
Url response = service.Url.Insert(new Url { LongUrl = url }).Execute();
That API is deprecated it no longer works.
Any alternative?
Yes Firebase Dynamic Links
[Transitioning Google URL Shortener to Firebase Dynamic Links](https://developers.googleblog.com/2018/03/transitioning-google-url-shortener.html)