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

半世苍凉 提交于 2020-02-04 04:05:25

问题


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.

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

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);
            }
        }

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");

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.

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

Appreciate it!


回答1:


After trial and error I finally figured it out.

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();

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



来源:https://stackoverflow.com/questions/49775790/how-to-use-google-safe-browsing-v4-with-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!