如何在 .NET 中使用 Google 安全浏览 (v4) [英] How to use Google Safe Browsing (v4) with .NET

查看:25
本文介绍了如何在 .NET 中使用 Google 安全浏览 (v4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Googles Safe Browsing Lookup API (v4, https://developers.google.com/safe-browsing/v4/lookup-api ) with a .NET application and had trouble finding example code.

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

I installed Google's nuget package but could find no examples on their github repo at https://github.com/google/google-api-dotnet-client

我能找到的最好的例子是在 https://developers.google.com/api-client-library/dotnet/get_started 但即使这样也不能准确显示我正在寻找什么.我只想查找 URL 的状态.以下是我从谷歌找到的唯一示例.

The best example I could find was at https://developers.google.com/api-client-library/dotnet/get_started but even that does not show me exactly what I am looking for. I just want to lookup what the status of a URL is. Below is the only example I found from google.

        // 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使用

I also tried a wrapper https://github.com/acastaner/safebrowsinglookup that looked fairly simple by using

 var client = new LookupClient("key", "dotnet-client");
 var response = await client.LookupAsync("http://amazon.com");

但这每次都返回未知".我确保我在 Google 上注册了一个新密钥,并授予它访问 Google Safe Browsing Api 4 的权限.

But this came back "unknown" every time. I made sure I registered a new key with google and gave it access to the Google Safe Browsing Api 4.

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

Any suggestions on how to use googles api to just get the response of one or many urls?

欣赏!

推荐答案

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

After trial and error I finally figured it out.

我的原始代码试图使用对我不起作用的 LookupClient.我通过查看谷歌如何初始化他们的发现服务并从那里构建了 FindthreatMatchesRequest()

My original code was attempting to use LookupClient which did not work for me. I found the solution by looking at how google initializes their Discovery Service and from there built out the 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 密钥

Hope this helps anyone looking for a quick solution. Don't forget to add your Api Key

这篇关于如何在 .NET 中使用 Google 安全浏览 (v4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆