我正在尝试将 Google 的安全浏览查找 API(v4,https://developers.google.com/safe-browsing/v4/lookup-api)与 .NET 应用程序结合使用,但找不到示例代码。
我安装了 Google 的 nuget 包,但在他们的 github 存储库 https://github.com/google/google-api-dotnet-client 上找不到任何示例。
我能找到的最好的例子是 https://developers.google.com/api-client-library/dotnet/get_started但即使这样也不能准确地告诉我我在寻找什么。我只想查找 URL 的状态。以下是我从谷歌找到的唯一示例。
// Create the service.
var service = new DiscoveryService(new BaseClientService.Initializer
{
ApplicationName = "Discovery Sample",
ApiKey="[YOUR_API_KEY_HERE]",
});
// Run the request.
Console.WriteLine("Executing a list request...");
var result = await service.Apis.List().ExecuteAsync();
// Display the results.
if (result.Items != null)
{
foreach (DirectoryList.ItemsData api in result.Items)
{
Console.WriteLine(api.Id + " - " + api.Title);
}
}
我还尝试了一个包装器 https://github.com/acastaner/safebrowsinglookup使用
看起来相当简单 var client = new LookupClient("key", "dotnet-client");
var response = await client.LookupAsync("http://amazon.com");
但这每次都返回“未知”。我确保我在谷歌注册了一个新 key ,并授予它访问 Google Safe Browsing Api 4 的权限。
关于如何使用 googles api 获取一个或多个 url 的响应有什么建议吗?
欣赏!
最佳答案
经过反复试验,我终于弄明白了。
我的原始代码试图使用对我不起作用的 LookupClient
。我通过查看 google 如何初始化他们的发现服务并从那里构建了 FindthreatMatchesRequest()
var service = new SafebrowsingService(new BaseClientService.Initializer
{
ApplicationName = "dotnet-client",
ApiKey = "API-KEY"
});
var request = service.ThreatMatches.Find(new FindThreatMatchesRequest()
{
Client = new ClientInfo
{
ClientId = "Dotnet-client",
ClientVersion = "1.5.2"
},
ThreatInfo = new ThreatInfo()
{
ThreatTypes = new List<string> { "Malware" },
PlatformTypes = new List<string> { "Windows" },
ThreatEntryTypes = new List<string> { "URL" },
ThreatEntries = new List<ThreatEntry>
{
new ThreatEntry
{
Url = "google.com"
}
}
}
});
var response = await request.ExecuteAsync();
希望这对寻求快速解决方案的任何人有所帮助。不要忘记添加您的 Api key
关于c# - 如何在 .NET 中使用 Google 安全浏览 (v4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775790/