簡體   English   中英

如何在 .NET 中使用 Google 安全瀏覽 (v4)

[英]How to use Google Safe Browsing (v4) with .NET

我正在嘗試將 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 的響應的任何建議?

欣賞它!

經過反復試驗,我終於弄明白了。

我的原始代碼試圖使用對我不起作用的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 密鑰

我通過尋找將我的應用程序與 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" },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM