3

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!

Garrett Vlieger
  • 9,354
  • 4
  • 32
  • 44
Alex J
  • 71
  • 6

2 Answers2

4

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Alex J
  • 71
  • 6
0

I found this thread by looking for a solution to integrate my app with Google SafeBrowsing. It worked for me, but I would like to add that I also added the line

return (FindThreatMatchesResponse)response;

at the end of the code posted above and wrapped it in a method called

protected async Task<FindThreatMatchesResponse> GoogleCheckAsync()

and then it my button click event added the following:

FindThreatMatchesResponse x = await GoogleCheckAsync();
string threatTypes;
if (x.Matches != null)
{
    foreach (ThreatMatch match in x.Matches)
    {
         threatTypes += match.ThreatType + ";";
    }
}

If you want to check for more suspicious sites, you might also want to replace the relevant part of code with this:

ThreatTypes = new List<string> { "Malware", "Social_Engineering", "Unwanted_Software", "Potentially_Harmful_Application" },
PlatformTypes = new List<string> { "Any_Platform" },
Orlyyn
  • 2,296
  • 2
  • 20
  • 32