3

我正在尝试将 Google 的安全浏览查找 API(v4,https://developers.google.com/safe-browsing/v4/lookup-api)与 .NET 应用程序一起使用,但无法找到示例代码。

我安装了 Google 的 nuget 包,但在https://github.com/google/google-api-dotnet-client的 github 存储库中找不到任何示例

我能找到的最好的例子是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");

但这每次都“未知”。我确保我在 google 上注册了一个新密钥并授予它对 Google Safe Browsing Api 4 的访问权限。

关于如何使用 googles api 来获取一个或多个 url 的响应的任何建议?

欣赏它!

4

2 回答 2

4

经过反复试验,我终于弄明白了。

我的原始代码试图使用LookupClient它对我不起作用。我通过查看谷歌如何初始化他们的发现服务找到了解决方案,并从那里构建了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

于 2018-04-11T19:42:11.697 回答
0

我通过寻找将我的应用程序与 Google SafeBrowsing 集成的解决方案找到了这个线程。它对我有用,但我想补充一点,我还添加了该行

return (FindThreatMatchesResponse)response;

在上面发布的代码的末尾并将其包装在一个名为的方法中

protected async Task<FindThreatMatchesResponse> GoogleCheckAsync()

然后我的按钮单击事件添加了以下内容:

FindThreatMatchesResponse x = await GoogleCheckAsync();
string threatTypes;
if (x.Matches != null)
{
    foreach (ThreatMatch match in x.Matches)
    {
         threatTypes += match.ThreatType + ";";
    }
}

如果您想检查更多可疑站点,您可能还希望将代码的相关部分替换为:

ThreatTypes = new List<string> { "Malware", "Social_Engineering", "Unwanted_Software", "Potentially_Harmful_Application" },
PlatformTypes = new List<string> { "Any_Platform" },
于 2020-08-10T06:19:10.117 回答