googleads/google-ads-python

google.api_core.exceptions.MethodNotImplemented: 501 GRPC target method can't be resolved

Opened this issue · 3 comments

Describe the bug:

Hello,

I'm new at Google API, and I was trying to test KeywordPlanIdeaService.
I try to follow the example provided in your doc (https://developers.google.com/google-ads/api/docs/keyword-planning/generate-keyword-ideas?hl=fr#python), removing stuff about which I don't know values to give.

But I get this error:
google.api_core.exceptions.MethodNotImplemented: 501 GRPC target method can't be resolved.

Steps to Reproduce:

Here is the final code:

from google.ads.googleads.client import GoogleAdsClient
import json

class ApiGoogleAds:
CREDENTIALS_FILE = "config/google-ads-credentials.json"
API_KEY = "..." # Api_key in google console for public data
credentials = None
client = None

def __init__(
    self,
):
    with open(self.CREDENTIALS_FILE, "r") as outfile:
        self.credentials = json.load(outfile)

    self.client = GoogleAdsClient.load_from_storage(self.CREDENTIALS_FILE)

def get_keyword_ideas(self, keywords: str = "", url: str = ""):
    keyword_plan_idea_service = self.client.get_service("KeywordPlanIdeaService")
    keyword_plan_network = (
        self.client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH_AND_PARTNERS
    )

    if not (keywords or url):
        raise ValueError(
            "At least one of keywords or page URL is required, "
            "but neither was specified."
        )

    request = self.client.get_type("GenerateKeywordIdeasRequest")
    request.customer_id = self.credentials["login_customer_id"]
    request.include_adult_keywords = False
    request.keyword_plan_network = keyword_plan_network

    if keywords and url:
        request.keyword_and_url_seed.url = url
        request.keyword_and_url_seed.keywords.extend(keywords)
    elif keywords:
        request.keyword_seed.keywords.extend(keywords)
    elif url:
        request.url_seed.url = url

    keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(
        request=request
    )

    for idea in keyword_ideas:
        competition_value = idea.[keyword_idea_metrics.competition.name](http://keyword_idea_metrics.competition.name/)
        print(
            f'Keyword idea text "{idea.text}" has '
            f'"{idea.keyword_idea_metrics.avg_monthly_searches}" '
            f'average monthly searches and "{competition_value}" '
            "competition.\n"
        )

if name == "main":
...

And then, my testing script :

python -m scripts.test_google_ads

from crawlers.api.api_google_ads import ApiGoogleAds

apiAds = ApiGoogleAds()
apiAds.get_keyword_ideas(keywords="bottes en cuir")

Expected behavior:

No error, or clear params/config error if I missed some params.

Client library version and API version:
Client library version: Python 3.9.18
Google Ads API version: google-ads==21.3.0

Request/Response Logs:

And when I run it, I have the following exception:
Request made: ClientCustomerId: 2416036531, Host: googleads.googleapis.com, Method: /google.ads.googleads.v14.services.KeywordPlanIdeaService/GenerateKeywordIdeas, RequestId: None, IsFault: True, FaultMessage: GRPC target method can't be resolved.
Traceback (most recent call last):
File "/Projects/data/venv/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 75, in error_remapped_callable
return callable_(*args, **kwargs)
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 277, in call
response, ignored_call = self._with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 332, in _with_call
return call.result(), call
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_channel.py", line 438, in result
raise self
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 315, in continuation
response, call = self._thunk(new_method).with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 343, in with_call
return self._with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 332, in _with_call
return call.result(), call
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_channel.py", line 438, in result
raise self
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 315, in continuation
response, call = self._thunk(new_method).with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 343, in with_call
return self._with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 329, in _with_call
call = self._interceptor.intercept_unary_unary(
File "/Projects/data/venv/lib/python3.9/site-packages/google/ads/googleads/interceptors/exception_interceptor.py", line 99, in intercept_unary_unary
self._handle_grpc_failure(response)
File "/Projects/data/venv/lib/python3.9/site-packages/google/ads/googleads/interceptors/exception_interceptor.py", line 71, in _handle_grpc_failure
raise self._get_error_from_response(response)
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_interceptor.py", line 315, in continuation
response, call = self._thunk(new_method).with_call(
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_channel.py", line 1178, in with_call
return _end_unary_response_blocking(state, call, True, None)
File "/Projects/data/venv/lib/python3.9/site-packages/grpc/_channel.py", line 1004, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNIMPLEMENTED
details = "GRPC target method can't be resolved."
debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B2a00:1450:4006:804::200a%5D:443 {created_time:"2024-11-15T14:43:51.013252+01:00", grpc_status:12, grpc_message:"GRPC target method can't be resolved."}"

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

Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.18_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/Cellar/python@3.9/3.9.18_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Projects/data/scripts/test_google_ads.py", line 5, in
apiAds.get_keyword_ideas(keywords="bottes en cuir")
File "/Projects/data/crawlers/api/api_google_ads.py", line 70, in get_keyword_ideas
keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(
File "/Projects/data/venv/lib/python3.9/site-packages/google/ads/googleads/v14/services/services/keyword_plan_idea_service/client.py", line 455, in generate_keyword_ideas
response = rpc(
File "/Projects/data/venv/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 131, in call
return wrapped_func(*args, **kwargs)
File "/Projects/data/venv/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 77, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.MethodNotImplemented: 501 GRPC target method can't be resolved.

I tried to look a bit if I found similar troubles reports, but either it's not matching mine, or I misunderstand things...

Could you please help me a bit ?
Sorry if my knowledge level about Google tools, even just development skills, are not very good, or even my english level.

Anything else we should know about your project / environment:

Thanks for the help you will provide.

Same behavior here! Need a fix!

Same here. Anyone able to fix it?

I found a way to use API by REST Interface. In order to help users:

base_url = "https://googleads.googleapis.com"
token_url = "https://oauth2.googleapis.com/token"
keywords_endpoint = f"{self.base_url}/v16/customers/{self.credentials['login_customer_id']}:generateKeywordIdeas"