【问题标题】:How to use Google Safe Browsing (v4) with .NET如何在 .NET 中使用 Google 安全浏览 (v4)
【发布时间】:2018-04-11 12:57:27
【问题描述】:

我正在尝试将 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 的状态。以下是我从 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,使用它看起来相当简单

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

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

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

欣赏!

【问题讨论】:

    标签: c# .net api google-api safe-browsing


    【解决方案1】:

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

    我的原始代码尝试使用 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

    【讨论】:

      【解决方案2】:

      我通过寻找将我的应用与 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" },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        • 2011-11-09
        相关资源
        最近更新 更多