googleads / googleads-python-lib

The Python client library for Google's Ads APIs
Apache License 2.0
683 stars 975 forks source link

KeyError occured when trying to access the 'report_service.runReportJob()' function #537

Closed SolomonPaulRDMediahuis closed 11 months ago

SolomonPaulRDMediahuis commented 11 months ago

This error appears to be a KeyError, which is recognized in the dictionary or data structure might be missing in the dictionary 'report_definition' while trying to access the 'report_service.runReportJob()' function. Below is my code and the error from googleads import ad_manager

application_name = 'GAM_SPaul_APP' network_code = '7380' path_to_private_key_file = 'forecast_inm_API.json' target_preset_id = '113171382953'

client = ad_manager.AdManagerClient.LoadFromString(f""" ad_manager:  application_name: {application_name}  network_code: {network_code}  path_to_private_key_file: {path_to_private_key_file} """)

report_definition = { 'reportType': 'STANDARD', 'dateRangeType': 'RELATIVE_PAST_7_DAYS', 'dimensions': [{'name': 'targetPresetIds', 'value': target_preset_id}], 'metrics': [ {'name': 'impressions', 'type': 'STANDARD'}, {'name': 'users', 'type': 'STANDARD'} ] }

report_service = client.GetService('ReportService', version='v202308')

report_job_id = report_service.runReportJob(report_definition)

report_job = client.GetService('ReportJobService', version='v202308').getReportJob(report_job_id) while report_job.status != 'COMPLETED':   report_job = client.GetService('ReportJobService', version='v202308').getReportJob(report_job_id)   time.sleep(10)

report = client.GetService('ReportService', version='v202308').getReport(report_job_id)

for row in report.rows:   print('Impressions:', row['impressions'])   print('Users:', row['users'])

Below is the error, not sure what is thet he structure and spelling of the 'report_definition' dictionary to ensure the key 'reportType' is correctly defined before the 'runReportJob()' function. How can I fix this please ? anysuggestions please


KeyError Traceback (most recent call last) Cell In[13], line 27 16 report_definition = { 17 'reportType': 'STANDARD', 18 'dateRangeType': 'RELATIVE_PAST_7_DAYS', (...) 23 ] 24 } 26 report_service = client.GetService('ReportService', version='v202308') ---> 27 report_job_id = report_service.runReportJob(report_definition) 29 report_job_service = client.GetService('ReportJobService', version='v202308') 31 while True:

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:981, in ZeepServiceProxy._CreateMethod..MakeSoapRequest(args) 979 AddToUtilityRegistry('zeep') 980 soap_headers = self._GetZeepFormattedSOAPHeaders() --> 981 packed_args = self._PackArguments(method_name, args) 982 try: 983 return soap_service_method( 984 packed_args, _soapheaders=soap_headers)['body']['rval']

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:842, in ZeepServiceProxy._PackArguments(self, method_name, args, set_type_attrs) 839 # Get the params for the method to find the initial types to instantiate. 840 op_params = self.zeep_client.get_element( 841 '{%s}%s' % (self._GetBindingNamespace(), method_name)).type.elements --> 842 result = [self._PackArgumentsHelper(param, param_data, set_typeattrs) 843 for ((, param), param_data) in zip(op_params, args)] 844 return result

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:842, in (.0) 839 # Get the params for the method to find the initial types to instantiate. 840 op_params = self.zeep_client.get_element( 841 '{%s}%s' % (self._GetBindingNamespace(), method_name)).type.elements --> 842 result = [self._PackArgumentsHelper(param, param_data, set_typeattrs) 843 for ((, param), param_data) in zip(op_params, args)] 844 return result

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:887, in ZeepServiceProxy._PackArgumentsHelper(self, elem, data, set_type_attrs) 884 elem_type = elem.type 886 data_formatted = data.items() --> 887 packed_result = self._CreateComplexTypeFromData( 888 elem_type, type_override is not None, data_formatted, set_type_attrs) 889 elif isinstance(data, zeep.xsd.CompoundValue): 890 # Here the data is already a SOAP element but we still need to look 891 # through it in case it has been edited with Python dicts. 892 elem_type = data._xsd_type

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:949, in ZeepServiceProxy._CreateComplexTypeFromData(self, elem_type, type_is_override, data, set_type_attrs) 945 elem_arguments = dict(elem_type.elements) 947 # A post order traversal of the original data, need to instantiate from 948 # the bottom up. --> 949 instantiated_arguments = { 950 k: self._PackArgumentsHelper(elem_arguments[k], v, set_type_attrs) 951 for k, v in data if k != 'xsi_type'} 952 if set_type_attrs: 953 found_type_attr = next((e_name for ename, in elem_type.elements 954 if e_name.endswith('.Type')), None)

File /opt/conda/lib/python3.10/site-packages/googleads/common.py:950, in (.0) 945 elem_arguments = dict(elem_type.elements) 947 # A post order traversal of the original data, need to instantiate from 948 # the bottom up. 949 instantiated_arguments = { --> 950 k: self._PackArgumentsHelper(elem_arguments[k], v, set_type_attrs) 951 for k, v in data if k != 'xsi_type'} 952 if set_type_attrs: 953 found_type_attr = next((e_name for ename, in elem_type.elements 954 if e_name.endswith('.Type')), None)

KeyError: 'reportType'

christopherseeley commented 11 months ago

The reference documentation for the ReportQuery is available here: https://developers.google.com/ad-manager/api/reference/latest/ReportService.ReportQuery

And a developer guide is available here: https://developers.google.com/ad-manager/api/reporting

There is also example code in this repo to help get started: https://github.com/googleads/googleads-python-lib/blob/40.0.0/examples/ad_manager/v202311/report_service/run_inventory_report.py

If you need additional help, you can reach out on our support forum: https://groups.google.com/g/google-doubleclick-for-publishers-api