Google ads API - REST API
Hope you all are doing well. I am new working with Google ads api. I have to retrieve information regarding keywords i.e how many people searched certain keywords , how many clicks and so on... so I have created a manager account on Google ads and under that I have created client account. In client account I have added keywords under keyword planner and I am getting all information mentioned above but I want to get it through REST API in python.
I have everything needed to access API: (Developer token login_customer_id Client ID Client Secret refresh token) I have given this information in the .yaml file. and I assume login_customer_id is the manager account id. Below is the code to access all the keywords information. here I have given the client_idfrom which I want to access keywords information.
import argparse import sys from google.ads.googleads.client import GoogleAdsClient from google.ads.googleads.errors import GoogleAdsException
def main(client, customer_id): ga_service = client.get_service("GoogleAdsService")
query = """ SELECT campaign.id, campaign.name, ad_group.id, ad_group.name, ad_group_criterion.criterion_id, ad_group_criterion.keyword.text, ad_group_criterion.keyword.match_type, metrics.impressions, metrics.clicks, metrics.cost_micros FROM keyword_view WHERE segments.date DURING LAST_7_DAYS AND campaign.advertising_channel_type = 'SEARCH' AND ad_group.status = 'ENABLED' AND ad_group_criterion.status IN ('ENABLED', 'PAUSED') ORDER BY metrics.impressions DESC LIMIT 50""" # Issues a search request using streaming. search_request = client.get_type("SearchGoogleAdsStreamRequest") search_request.customer_id = customer_id search_request.query = query response = ga_service.search_stream(search_request) for batch in response: for row in batch.results: campaign = row.campaign ad_group = row.ad_group criterion = row.ad_group_criterion metrics = row.metrics print( f'Keyword text "{criterion.keyword.text}" with ' f'match type "{criterion.keyword.match_type.name}" ' f"and ID {criterion.criterion_id} in " f'ad group "{ad_group.name}" ' f'with ID "{ad_group.id}" ' f'in campaign "{campaign.name}" ' f"with ID {campaign.id} " f"had {metrics.impressions} impression(s), " f"{metrics.clicks} click(s), and " f"{metrics.cost_micros} cost (in micros) during " "the last 7 days." ) # [END get_keyword_stats]
if name == "main":
googleads_client=GoogleAdsClient.load_from_storage("C:\Users\AnoshpaBansari\PycharmProjects\GoogleAPI\src\creds\googleads.yaml")
parser = argparse.ArgumentParser( description=("Retrieves a campaign's negative keywords.") ) # The following argument(s) should be provided to run the example. #parser.add_argument( # "-c", # "--customer_id", # type=str, #required=True, #help="The Google Ads customer ID.", #) #args = parser.parse_args() try: main(googleads_client, "----------") except GoogleAdsException as ex: print( f'Request with ID "{ex.request_id}" failed with status ' f'"{ex.error.code().name}" and includes the following errors:' ) for error in ex.failure.errors: print(f'\tError with message "{error.message}".') if error.location: for field_path_element in error.location.field_path_elements: print(f"\t\tOn field: {field_path_element.field_name}") sys.exit(1)
but when I run the code I receive this error. I don't know what I am doing wrong.. Can anyone please help? enter image description here
You must login in Google Ads Manager accounts, go Tools & Settings > API Center and accept the API terms and conditions.