Search code examples
c#google-apigoogle-api-dotnet-clientgoogle-custom-search

Google API Client returning 404 not fund


I am trying to get the best searches from google, using keyword, and GoogleAPI, but It is always returning "System.Net.WebException: 'The remote server returned an error: (404) Not Found.'"

Am I doing anything wrong, or is it that GoogleAPI is outdated? Last time I checked and downloaded the .dll file was from 2010...

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using Microsoft.Win32;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
using System.ComponentModel;
using System.Net;
using System.Windows;
using Google.Apis.Services;
using Google.Apis.CustomSearchAPI.v1;

//Ignore most using statements, It was imported from my previous code.

namespace TYZDsag
{
   
    internal class Program
    {
        static void Main(string[] args)
        {
            var client = new Google.API.Search.GwebSearchClient("https://www.google.com/");
            var results = client.Search("uhuhuh", 12);
            foreach (var webResult in results)
            {
                Console.WriteLine("{0}, {1}, {2}", webResult.Title, webResult.Url, webResult.Content);
                //listBox1.Items.Add(webResult.ToString());
            }

        }
    }
}


Solution

  • That's not how you initialize a service object using an api key.

    using System;
    using System.Threading.Tasks;
    using Google.Apis.CustomSearchAPI.v1;
    using Google.Apis.Services;
    
    namespace customsearch
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Console.WriteLine("Hello World!");
    
                var service = new CustomSearchAPIService(new BaseClientService.Initializer()
                {
                    ApiKey = "XXX",
                    ApplicationName = "xyz",
    
                });
    
                var result = await service.Cse.List().ExecuteAsync();
    
            }
        }
    }