如何使用asp.net mvc以编程方式缩短使用google shortener的URL?

时间:2017-02-27 08:51:38

标签: c# asp.net-mvc goo.gl

SO。我有来自通过API调用访问的第三方的以下URL。

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

我想从我的操作方法中缩短使用Google缩短程序的网址,我应该怎么做?

注意:安装了goo.gl shortner nuget包。

1 个答案:

答案 0 :(得分:2)

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

}