Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Google shortenurl API制作短URL_C#_Google Api_Google Url Shortener - Fatal编程技术网

C# 使用Google shortenurl API制作短URL

C# 使用Google shortenurl API制作短URL,c#,google-api,google-url-shortener,C#,Google Api,Google Url Shortener,我正在尝试使用谷歌缩短url来缩短我们项目的许多url,并不断得到一个未处理的HTTPRequestException错误。我第一次运行它时,它要求定位一个不在那里的.cs文件,所以我猜这就是原因。我刚刚在VisualStudio中使用了nugget安装程序。有什么想法吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Task

我正在尝试使用谷歌缩短url来缩短我们项目的许多url,并不断得到一个未处理的HTTPRequestException错误。我第一次运行它时,它要求定位一个不在那里的.cs文件,所以我猜这就是原因。我刚刚在VisualStudio中使用了nugget安装程序。有什么想法吗

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;
    }
}
}
//在我的例子中可以使用的代码:)
//VS2017
私有静态字符串缩短(字符串url)
{
UrlshortenerService=新的UrlshortenerService(
新的BaseClientService.Initializer(){
ApiKey=“”,
ApplicationName=“”,});
var m=new Google.api.Urlshortener.v1.Data.Url();
m、 LongUrl=url;
return service.Url.Insert(m.Execute().Id;
}

您应该更具体一些。至少完整的异常信息。System.Net.Http.HttpRequestException:发送请求时出错。-->System.Net.WebException:远程服务器返回错误:(407)需要代理身份验证。在System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)---内部异常堆栈跟踪的结尾---Google.API.Requests.ClientServiceRequest`1.Execute()处的System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext和context)在C:\apariy\v1.22\google api dotnet client\Src\Support\GoogleAppis\api\Requests\ClientServiceRequest.cs:C:\Users\justinwendo\Desktop\test\U VB\U projects\ShortenURL\Program中ShortenURL.Program.Shorten(字符串url)的第101行发送请求时出错。在C:\apariy\v1.22\Google api dotnet client\Src\Support\GoogleAppis\Apis\Requests\ClientServiceRequest.cs中的Google.api.Requests.ClientServiceRequest`1.Execute():ShortenURL.Program.Shorten(字符串url)的第101行在C:\Users\justinwendo\Desktop\test\u VB\u projects\ShortenURL\ShortenURL\Program.cs:37行,很抱歉,由于字符太多,无法让我一次发布所有内容。
    //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;
    }