2

I used Google Cloud Platform for Free trial https://cloud.google.com/free-trial/

to use custom search API in my java application

I write the following code in my application

 URL url = new URL("https://www.googleapis.com/customsearch/v1?key="+key+ 
 "&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");

while ((output = br.readLine()) != null) {

    if(output.contains("\"link\": \"")){                
        String link=output.substring(output.indexOf("\"link\": \"")+("\"link\": \"").length(), output.indexOf("\","));

        System.out.println(link);       //Will print the google search links
    }     
}
conn.disconnect();       

it some times gave me a result and after that it gave me the following error , and this error occurred regardless the query string

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.googleapis.com/customsearch/v1?key=????&cx=013036536707430787589:_pqjad5hr1a&q=????? at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

Where is the problem please?

TDeveloper
  • 23
  • 3

1 Answers1

0

403 is specifically Google's forbidden error - it means your sending too many requests too quickly and they think you using a bot or automation

If you load that query in your browser and watch the requests - keep hitting refresh quickly until you see an html response that says

We're sorry...

... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

See Google Help for more information.
Aurielle Perlmann
  • 5,323
  • 1
  • 15
  • 26