googleads / google-ads-python

Google Ads API Client Library for Python
Apache License 2.0
515 stars 480 forks source link

AttributeError 'META' #421

Closed Get455by closed 3 years ago

Get455by commented 3 years ago

Good afternoon. I am a beginner Django Python super master. I face big problems every day, but there is one big problem that I can't solve for more than 2 days. I'm trying to make a project using the google ads api. I have completed all the necessary procedures and can connect to test accounts to receive information and make changes to test accounts. But when trying to port this to django-I ran into problems. My last solution works at 50%. I get the necessary information and immediately get an additional ERROR.

the essence: in views.py I call a function that connects to a test account to collect statistics for a given keyword. I get statistics, and an error. I will be glad of any help or hint) thank you!

AttributeError at /faq 'META' Request Method: GET Request URL: http://localhost:8000/faq Django Version: 3.2 Exception Type: AttributeError Exception Value:
'META' Exception Location: F:\python\apiserv_website\myvenv\lib\site-packages\proto\message.py, line 605, in getattr Python Executable: F:\python\apiserv_website\myvenv\Scripts\python.exe Python Version: 3.9.3 Python Path:
['F:\python\apiserv_website\apiserv', 'C:\Users\romar\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\romar\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\romar\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\romar\AppData\Local\Programs\Python\Python39', 'F:\python\apiserv_website\myvenv', 'F:\python\apiserv_website\myvenv\lib\site-packages'] Server time: Wed, 05 May 2021 23:13:14 +0000

views.py
the code inside the function I'm calling:

credentials = { "developer_token": "hide", "refresh_token": "hide", "client_id": "hide", "client_secret": "hide" }

client = GoogleAdsClient.load_from_dict(credentials)

keyword_texts = ['slippers']
language_id = "1000"
location_ids = "1023191"
customer_id = "4677556196"

def _map_locations_ids_to_resource_names(client, location_ids):
    """Converts a list of location IDs to resource names.

    Args:
        client: an initialized GoogleAdsClient instance.
        location_ids: a list of location ID strings.

    Returns:
        a list of resource name strings using the given location IDs.
    """
    build_resource_name = client.get_service(
        "GeoTargetConstantService"
    ).geo_target_constant_path
    return [build_resource_name(location_id) for location_id in location_ids]

keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService")
keyword_competition_level_enum = client.get_type(
    "KeywordPlanCompetitionLevelEnum"
).KeywordPlanCompetitionLevel
keyword_plan_network = client.get_type(
    "KeywordPlanNetworkEnum"
).KeywordPlanNetwork.GOOGLE_SEARCH_AND_PARTNERS
language_rn = client.get_service(
    "LanguageConstantService"
).language_constant_path(language_id)

request = client.get_type("GenerateKeywordIdeasRequest")
request.customer_id = customer_id
request.language = language_rn
request.include_adult_keywords = False
request.keyword_plan_network = keyword_plan_network

request.keyword_seed.keywords.extend(keyword_texts)

keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(
    request=request
)
limit = 20
index = 0
goog_key_tool = []
for idea in keyword_ideas:
    competition_value = idea.keyword_idea_metrics.competition.name
    goog_key_tool.append(idea.keyword_idea_metrics.avg_monthly_searches)
    index += 1
    if index == limit:
        break
print(goog_key_tool)

error screenshot

BenRKarl commented 3 years ago

@Get455by thanks for providing these details, they're very helpful! When I run your script as-is I don't get any errors, so I don't think the problem has to do with that logic.

What the error message tells me is that somewhere in your codebase you're accessing the META attribute on a protobuf message class instance, which are the objects you receive from service responses, as well as from calling client.get_type and client.get_service. Do you know anywhere else in your application where you might have such logic?

Get455by commented 3 years ago

unfortunately, no, I don't know. I posted it on the site. to see if there will be the same type of error. perhaps if you look at the link - you will see more information? (just through the terminal, everything works fine for me too) but the localhost and the site catches the error(( https://gardensoldier.com/faq -- the entire site is an exact copy of the one on the localhost

Get455by commented 3 years ago

the fact is that the site is completely naked. there is a single view facing the etsy api. but there are no such intricate intricacies as with google api)

BenRKarl commented 3 years ago

@Get455by Seems like this Stack post might help. Can you try renaming your request variable in the above script to see if that fixes the issue?

Get455by commented 3 years ago

Thank you very much. I certainly saw this solution, but apparently I wasn't ready to apply it yet! Thank you, thank you, thank you. I am the happiest novice programmer on earth today)

here is my new code in the function:


requestrename = client.get_type("GenerateKeywordIdeasRequest")
requestrename.customer_id = customer_id
requestrename.language = language_rn
requestrename.include_adult_keywords = False
requestrename.keyword_plan_network = keyword_plan_network

requestrename.keyword_seed.keywords.extend(keyword_texts)

keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(
    request=requestrename
    ----------------------------------------
 both on the locale and on the site works perfectly!
BenRKarl commented 3 years ago

Wonderful, glad I could help!