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

查看:67
本文介绍了如何在.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的状态.以下是我从Google找到的唯一示例.

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安全浏览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仅获得一个或多个网址的响应的任何建议吗?

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,但该代码对我不起作用.我通过查看Google如何初始化其发现服务并从那里构建了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天全站免登陆