0

我正在尝试使用谷歌缩短网址来缩短我们项目的大量网址并不断收到 HTTPRequestException 未处理的错误。我第一次运行它时,它要求找到一个不存在的 .cs 文件,所以我猜是因为这个。我只是使用 nugget 安装程序在 Visual Studio 中获取它。有任何想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Urlshortener.v1;
using Google.Apis.Oauth2;
using Google.Apis.Services;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ShortenURL
{
class Program
{
    static void Main(string[] args)
    {
        var originalURL = "https://www.google.com/maps/place/Desert+Christian+Schools/@32.2367293,-110.8339121,16.75z/data=!4m7!1m4!3m3!1s0x86d66f1806d996d7:0xe8ac20e8cebb38b9!2s7525+E+Speedway+Blvd,+Tucson,+AZ+85710!3b1!3m1!1s0x86d66f1806d996d7:0x7b90764e4e6a25d8";
        string shortUrl = Shorten(originalURL);
        Console.WriteLine(shortUrl);
        Console.WriteLine("FINISHED");
        Console.ReadLine();
    }
    private const string key = "AIzaSyB3pfstkvAZzEVOy4dNHaKTuNmtDaG3XsI";
    public static string Shorten(string url)
    {
        UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
        {
            ApiKey = key,
            ApplicationName = "ShortenUrlAHLI"
        });
        var m = new Google.Apis.Urlshortener.v1.Data.Url();
        m.LongUrl = url;
        return service.Url.Insert(m).Execute().Id;
    }
}
}
4

1 回答 1

0
    //The code workable in my case :)  
    //VS2017        

    private static string shorten(string url)
    {
        UrlshortenerService service = new UrlshortenerService(
                new BaseClientService.Initializer() {
                ApiKey = "<google-api-key>",
                ApplicationName = "<google-app-id>", });
        var m = new Google.Apis.Urlshortener.v1.Data.Url();
        m.LongUrl = url;

        return service.Url.Insert(m).Execute().Id;
    }
于 2017-06-02T12:37:35.587 回答