Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Steps for using Google custom search API in .NET

I am trying to use Google custom search API in my .NET project. I have an API Key provided by my company. I have created a custom search engine using my Google account and copied the 'cx' value.

I am using the following code:

string apiKey = "My company Key";
string cx = "Cx";
string query = tbSearch.Text;

WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Only a test!");

string result = webClient.DownloadString(String.Format("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}&alt=json", apiKey, cx, query));

I am getting the following error: "The remote server returned an error: (403) Forbidden. "

I have tried the following code too:

Google.Apis.Customsearch.v1.CustomsearchService svc = new Google.Apis.Customsearch.v1.CustomsearchService();
svc.Key = apiKey;

Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = svc.Cse.List(query);
listRequest.Cx = cx;
Google.Apis.Customsearch.v1.Data.Search search = listRequest.Fetch();

foreach (Google.Apis.Customsearch.v1.Data.Result result1 in search.Items)
{
   Console.WriteLine("Title: {0}", result1.Title);
   Console.WriteLine("Link: {0}", result1.Link);
}

Here I get the following exception at Fetch():

Google.Apis.Requests.RequestError Access Not Configured [403] Errors [Message[Access Not Configured] Location[ - ] Reason[accessNotConfigured] Domain[usageLimits]

Is CX parameter required? Am I getting the error because I am using the Key provided by my company and using the CX parameter from custom search engine using my Google account?

Is there any other way of getting 'cx'? We don't want to display Google ADs.

Thank you very much in advance for help.

like image 624
Raj Avatar asked Feb 14 '13 17:02

Raj


People also ask

How does Google Custom Search work?

Google's custom search comes with a selection of pre-built themes, but it is possible to customize the design and style of the search box and results page so they match the website's overall theme.

Is there an API for Google search results?

The Search Console API provides programmatic access to the most popular reports and actions in your Search Console account. Query your search analytics, list your verified sites, manage your sitemaps for your site, and more. Official Google Search updates and SEO best practices.


2 Answers

I'm not sure if you are still interested in this.

To get results without ads you need to pay for it. Info @ Google

and yes the cx is required because it specifies the google custom search engine that you want to use to search. you can create a custom search engine from This google page

and here is the current code to retrieve search results for the current api version 1.3.0-beta

        string apiKey = "Your api key";
        string cx = "Your custom search engine id";
        string query = "Your query";

        var svc = new Google.Apis.Customsearch.v1.CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey });
        var listRequest = svc.Cse.List(query);

        listRequest.Cx = cx;
        var search = listRequest.Fetch();

        foreach (var result in search.Items)
        {
            Response.Output.WriteLine("Title: {0}", result.Title);
            Response.Output.WriteLine("Link: {0}", result.Link);
        }

Hope this helps

like image 66
Mahmoud Ibrahim Avatar answered Sep 24 '22 19:09

Mahmoud Ibrahim


Instead of,

var search = listRequest.Fetch();

But now it does not supports Fetch() method, rather you need to use Execute() method.

var search = listRequest.Execute();
like image 27
Kumar Ambuj Avatar answered Sep 22 '22 19:09

Kumar Ambuj