BingAds / BingAds-Python-SDK

Other
117 stars 162 forks source link

bingads report download Error: There was an error deserializing the object, cannot be parsed as the type 'Int32' #148

Closed ali-raza-hafiz closed 4 years ago

ali-raza-hafiz commented 4 years ago

I am using bingads.v13, python 2.7 for reporting. Here is my code:

REPORT_FILE_FORMAT='Csv'
FILE_DIRECTORY='/'
RESULT_FILE_NAME='result.' + REPORT_FILE_FORMAT.lower()
TIMEOUT_IN_MILLISECONDS=3600000
aggregation = 'Daily'
exclude_column_headers=False
exclude_report_footer=False
exclude_report_header=False
authorization_data = BingAPIHandler.get_bing_service(
        "ReportingService",
        customerId,
    )
time=reporting_service.factory.create('ReportTime')
time.PredefinedTime='Yesterday'
time.ReportTimeZone='PacificTimeUSCanadaTijuana'
return_only_complete_data=False
reporting_service_manager=ReportingServiceManager(
    authorization_data=authorization_data, 
    poll_interval_in_milliseconds=5000, 
    environment=settings.LIVE_ENVIRONMENT,
)
def get_campaign_performance_report_request(account_id,aggregation,exclude_column_headers,exclude_report_footer,exclude_report_header,report_file_format,return_only_complete_data,time):
    report_request=reporting_service.factory.create('CampaignPerformanceReportRequest')
    report_request.Aggregation=aggregation
    report_request.ExcludeColumnHeaders=exclude_column_headers
    report_request.ExcludeReportFooter=exclude_report_footer
    report_request.ExcludeReportHeader=exclude_report_header
    report_request.Format=report_file_format
    report_request.ReturnOnlyCompleteData=return_only_complete_data
    report_request.Time=time    
    report_request.ReportName="My Campaign Performance Report"
    scope=reporting_service.factory.create('AccountThroughCampaignReportScope')
    scope.AccountIds={'long': [account_id] }
    scope.Campaigns=None
    report_request.Scope=scope     
   report_columns=reporting_service.factory.create('ArrayOfCampaignPerformanceReportColumn')
    report_columns.CampaignPerformanceReportColumn.append([
        'TimePeriod',
        'CampaignId',
        'CampaignName',
        'DeviceType',
        'Network',
        'Impressions',
        'Clicks',  
        'Spend'
    ])
    report_request.Columns=report_columns
    return report_request
def download_report(reporting_download_parameters):
    global reporting_service_manager
    report_container = reporting_service_manager.download_report(reporting_download_parameters)
    if(report_container == None):
        output_status_message("There is no report data for the submitted report request parameters.")
        sys.exit(0)
    record_count = report_container.record_count
    output_status_message("ReportName: {0}".format(report_container.report_name))
    output_status_message("ReportTimeStart: {0}".format(report_container.report_time_start))
    output_status_message("ReportTimeEnd: {0}".format(report_container.report_time_end))
    output_status_message("LastCompletedAvailableDate: {0}".format(report_container.last_completed_available_date))
    output_status_message("ReportAggregation: {0}".format(report_container.report_aggregation))
    output_status_message("ReportColumns: {0}".format("; ".join(str(column) for column in report_container.report_columns)))
    output_status_message("ReportRecordCount: {0}".format(record_count))
    if "Impressions" in report_container.report_columns and "Clicks" in report_container.report_columns and "DeviceType" in report_container.report_columns and "Network" in report_container.report_columns:
        report_record_iterable = report_container.report_records
        total_impressions = 0
        total_clicks = 0
        distinct_devices = set()
        distinct_networks = set()
        for record in report_record_iterable:
            total_impressions += record.int_value("Impressions")
            total_clicks += record.int_value("Clicks")
            distinct_devices.add(record.value("DeviceType"))
            distinct_networks.add(record.value("Network"))
        output_status_message("Total Impressions: {0}".format(total_impressions))
        output_status_message("Total Clicks: {0}".format(total_clicks))
        output_status_message("Average Impressions: {0}".format(total_impressions * 1.0 / record_count))
        output_status_message("Average Clicks: {0}".format(total_clicks * 1.0 / record_count))
        output_status_message("Distinct Devices: {0}".format("; ".join(str(device) for device in distinct_devices)))
        output_status_message("Distinct Networks: {0}".format("; ".join(str(network) for network in distinct_networks)))
    report_container.close()
campaign_performance_report_request=get_campaign_performance_report_request(
    account_id=customerId,
    aggregation=aggregation,
    exclude_column_headers=exclude_column_headers,
    exclude_report_footer=exclude_report_footer,
    exclude_report_header=exclude_report_header,
    report_file_format=REPORT_FILE_FORMAT,
    return_only_complete_data=return_only_complete_data,
    time=time
)
reporting_download_parameters = ReportingDownloadParameters(
    report_request=campaign_performance_report_request,
    result_file_directory = FILE_DIRECTORY, 
    result_file_name = RESULT_FILE_NAME, 
    overwrite_result_file = True, 
    timeout_in_milliseconds=TIMEOUT_IN_MILLISECONDS 
)

When i download report

download_report(reporting_download_parameters)

i am facing this error from server :

WebFault: Server raised fault: 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter https://bingads.microsoft.com/Reporting/v13:ReportRequest. The InnerException message was 'There was an error deserializing the object of type Microsoft.AdCenter.Advertiser.Reporting.Api.DataContracts.Request.ReportRequest. The value '' cannot be parsed as the type 'Int32'.'. Please see InnerException for more details.

qitia commented 4 years ago

@ali-raza-hafiz , try time.CustomDateRangeStart = None time.CustomDateRangeEnd = None

let me know if it helps.

qitia commented 4 years ago

Close as of now @ali-raza-hafiz, I believe you should be good with the suggestions above. Please reopen this issue otherwise.

Ritik262 commented 2 years ago

How to find msclkid in bing ads using python api