Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java google custom search api

I'm trying to use the java client for the Google custom search api but couldn't find any sample tutorials on the web. Can someone provide a simple example for me to get started? Thank you!

like image 697
wolve80 Avatar asked Aug 02 '11 19:08

wolve80


People also ask

How do I get a Google custom search API?

With Google Cloud Operations you can create custom dashboards, set up alerts, and access metrics data programmatically. To access Custom Search JSON API usage data in Google Cloud Operations, select "Resource type: Consumed API" and filter on "service = 'customsearch.googleapis.com'" in the Query Builder.

Is there an API for Google Search?

Google Web Search API has been deprecated and replaced with Custom Search API (see http://code.google.com/apis/websearch/).

How do I get a custom search API key?

You can get an API key by visiting https://code.google.com/apis/console and clicking "API Access". You will then need to switch on the custom search API on the "Services" tab.


1 Answers

I want to make a correction here.

customsearch.setKey("YOUR_API_KEY_GOES_HERE");

does not work for client lib 1.6 but following does work

     Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());

    try {
        com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
        list.setKey("YOUR_API_KEY_GOES_HERE");
        list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE");
        Search results = list.execute();
        List<Result> items = results.getItems();

        for(Result result:items)
        {
            System.out.println("Title:"+result.getHtmlTitle());

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
like image 79
subha Avatar answered Sep 18 '22 14:09

subha