Search code examples
c#asp.net-mvcgoo.gl

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


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.


Solution

  • 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();
        }
    
    }