javagoogle-apisafe-browsing

Google Safe Browsing HTTP POST - 403 response


I'm working on a program that queries Google Safe Browsing for certain urls, but I'm getting an error that I don't think I should be getting.

I'm sending the following request:

2
http://google.com
http://facebook.com

via POST to: https://sb-ssl.google.com/safebrowsing/api/lookup?client=api&apikey=[KEY]&appver=1.5.2&pver=3.1

However, I'm getting a 403 response.

This is what the documentation says for HTTP POST lookup errors:

The server generates the following HTTP error codes for the POST request:

•200: AT LEAST ONE of the queried URLs are matched in either the phishing, malware, or unwanted software lists. The actual results are returned through the response body.

•204: NONE of the queried URLs matched the phishing, malware, or unwanted software lists, and no response body is returned.

•400: Bad Request—The HTTP request was not correctly formed.

•401: Not Authorized—The API key is not authorized.

•503: Service Unavailable—The server cannot handle the request. Besides the normal server failures, this could also indicate that the client has been “throttled” for sending too many requests.

The response code 403 isn't listed, yet I'm getting it.

I have triple-checked my API-key and made sure the API is enabled for my project. I'm using a Server-key, but I also tried a Browser-key.

I tried doing a GET request also, and that did work, but I cannot get POST to work. What's going on?

Here is my code:

try {
   String baseURL="https://sb-ssl.google.com/safebrowsing/api/lookup";
   String arguments = "";
   arguments +=URLEncoder.encode("client", "UTF-8") + "=" + URLEncoder.encode("api", "UTF-8") + "&";
   arguments +=URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode("[KEY]", "UTF-8") + "&";
   arguments +=URLEncoder.encode("appver", "UTF-8") + "=" + URLEncoder.encode("1.5.2", "UTF-8") + "&";
   arguments +=URLEncoder.encode("pver", "UTF-8") + "=" + URLEncoder.encode("3.1", "UTF-8");

   // Construct the url object representing cgi script
   URL url = new URL(baseURL + "?" + arguments);    

   // Get a URLConnection object, to write to POST method
   HttpURLConnection connect = (HttpURLConnection) url.openConnection();
   connect.setRequestMethod("POST");

   // Specify connection settings
   connect.setDoInput(true);
   connect.setDoOutput(true);

   // Get an output stream for writing
   OutputStream output = connect.getOutputStream();
   PrintStream pout = new PrintStream (output);
   pout.print("2");
   pout.println();
   pout.print("http://www.google.com");
   pout.println();
   pout.print("http://www.facebook.com");
   pout.close();                   

   BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));

   String decodedString;
   while ((decodedString = in.readLine()) != null) {
      System.out.println("w: " + decodedString);
   }
   in.close();
} catch(Exception e) {
   e.printStackTrace();
}

Solution

  • I found the error. The CGI parameter was incorrect. It should have been key and not apikey. Still weird that you get an undocumented response-code though.