How can I shorten a URL with google shortener programmatically using asp.net mvc?

2.6k Views Asked by At

SO. I have the following URL from a third party accessed with an API call.

https://scontent.xx.fbcdn.net/v/t1.0-9/15665479_1260320054027269_4201232212927955955_n.jpg?oh=ee01f2ec47b2e972bc12f99d988db241&oe=5946A159

I'd like to shorten this URL with Google shortener from inside my action method, how should i do this?

Note: The goo.gl shortner nuget package installed.

1

There are 1 best solutions below

7
Zbidi On BEST ANSWER
class Program
{ 
    static void Main(string[] args)
    {

       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 = @"https://scontent.xx.fbcdn.net/v/t1.0-9/15665479_1260320054027269_4201232212927955955_n.jpg?oh=ee01f2ec47b2e972bc12f99d988db241&oe=5946A159";
        var shortenedUrl =  service.Url.Insert(m).Execute().Id;

        Console.WriteLine(shortenedUrl);
        Console.ReadKey();
    }

}