Google安全浏览V4 - 无法发送请求

时间:2017-11-13 07:03:55

标签: asp.net-mvc google-api safe-browsing

我一直致力于谷歌安全浏览API - 我确信我正在做的一切正确,但是当我尝试连接到API时出现错误

  

“对象引用未设置为对象的实例。”

我安装了正确的nuGet包

我的代码如下

try
{
    //cient_info
    Google.Apis.Safebrowsing.v4.Data.ClientInfo client = new Google.Apis.Safebrowsing.v4.Data.ClientInfo();
    client.ClientId = "testapp";
    client.ClientVersion = "1";

    //thread_info
    Google.Apis.Safebrowsing.v4.Data.ThreatInfo threadInfo = new Google.Apis.Safebrowsing.v4.Data.ThreatInfo();
    threadInfo.ThreatTypes.Add("MALWARE");
    threadInfo.ThreatTypes.Add("SOCIAL_ENGINEERING");
    threadInfo.PlatformTypes.Add("WINDOWS");
    threadInfo.ThreatEntryTypes.Add("URL");

    //url to check
    Google.Apis.Safebrowsing.v4.Data.ThreatEntry ITEM = new Google.Apis.Safebrowsing.v4.Data.ThreatEntry();
    ITEM.Url = "http://www.google.com.au/";
    threadInfo.ThreatEntries.Add(ITEM);

    //API Call
    var googleClient = new WebClient();
    var response = googleClient.DownloadString("https://safebrowsing.googleapis.com/v4/" + client + threadInfo + "&key=myapikey");
    var releases = JObject.Parse(response);
    return releases.ToString();
}
catch (Exception X)
{
    var Error = X.Message;
    return Error.ToString();
}

我想我在var response = googleClient.DownloadString搞砸了,但我不确定这是什么样的正确呼叫方法。

有没有人有任何想法?

干杯

2 个答案:

答案 0 :(得分:1)

我使用了ThreatMatches.Find(),它起作用了。

ThreatMatchesResource.FindRequest request = service.ThreatMatches.Find(
new FindThreatMatchesRequest{Client = client, ThreatInfo = threatInfo});

FindThreatMatchesResponse execute = await request.ExecuteAsync();

var releases = execute.ToString();

答案 1 :(得分:0)

您需要将api密钥放入

googleClient.DownloadString("https://safebrowsing.googleapis.com/v4/" + client + threadInfo + "&key=myapikey");