arrrlo / Google-Images-Search

[PYTHON] Search for image using Google Custom Search API and resize & crop afterwards
MIT License
176 stars 34 forks source link

UnknownApiNameOrVersion: name: customsearch version: v1 #156

Closed AIManifest closed 1 year ago

AIManifest commented 1 year ago

nextcord.errors.ApplicationInvokeError: Command raised an exception: UnknownApiNameOrVersion: name: customsearch version: v1

Is this an issue on Google's end? Any help would be appreciated. Thanks in advance for your time.

JacobCll commented 1 year ago

Had the same issue in repl.it for a discord bot. Command runs fine in Vscode, but not there. Code:

@bot.command()
async def img(ctx, *, args):
    gis = GoogleImagesSearch(GOOGLE_API_KEY, CXID)

    _search_params = {
    'q': args,
    'num': 3
}
    gis.search(search_params=_search_params)

    image_list = []

    async with ctx.typing():
        for image in gis.results():
            image_list.append(image.url)
        print(image_list)

    await ctx.send(image_list[random.randint(0, len(image_list)-1)])

Error:

Ignoring exception in command img:
Traceback (most recent call last):
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 184, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 96, in img
    gis.search(search_params=_search_params)
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/google_images_search/fetch_resize_save.py", line 151, in search
    self._search_images(*self._get_data())
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/google_images_search/fetch_resize_save.py", line 177, in _search_images
    for url, referrer_url in self._google_custom_search.search(
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/google_images_search/google_api.py", line 83, in search
    res = self._query_google_api(search_params, cache_discovery)
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/google_images_search/google_api.py", line 43, in _query_google_api
    self._google_build = discovery.build(
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/googleapiclient/discovery.py", line 287, in build
    content = _retrieve_discovery_doc(
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/googleapiclient/discovery.py", line 404, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: customsearch  version: v1

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 342, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 951, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/DiscordBot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 193, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnknownApiNameOrVersion: name: customsearch  version: v1
arrrlo commented 1 year ago

Looks like sometimes Google API has problems with API discovery doc: https://github.com/googleworkspace/python-samples/issues/194 https://stackoverflow.com/questions/66700249/google-gmail-api-works-fine-as-py-but-throws-googleapiclient-errors-unknownap

This could be due to the google's dependency libs or something else. Try to play with cache_discovery argument when conducting the search:

gis = GoogleImagesSearch(GOOGLE_API_KEY, CXID)
_search_params = {...}
gis.search(search_params=_search_params, cache_discovery=True)
# the default is False