BingAds / BingAds-Python-SDK

Other
116 stars 162 forks source link

How to pass AdTypes in the GetAdsByAdGroupId() function #202

Closed rishaab closed 2 years ago

rishaab commented 2 years ago

My goal is to get a dataframe of ads and their meta information from the bingAds API.

I'm using the campaign management service to GetAdsByAdGroupId()

Following the documentation below: https://docs.microsoft.com/en-us/advertising/campaign-management-service/getadsbyadgroupid?view=bingads-13

I need to pass 2 arguments into GetAdsByAdGroupId() to build the request body elements

Element - Description - Data type

  1. AdGroupId - The identifier of the ad group to retrieve the ads from. - long
  2. AdTypes - One or more ad types to retrieve. - AdType array

Below is the code I've written:

    adtypes = ["Text", "Image", "Product", "AppInstall", "ExpandedText", "DynamicSearch", "ResponsiveAd", "ResponsiveSearch"]

    for adgrp_id in adgrp_id_list:

        ad_response = campaignmanagement_service.GetAdsByAdGroupId(
            AdGroupId=adgrp_id,
            AdTypes=adtypes
        )

However I'm getting the following error:

DEBUG:suds.client:headers = {'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': b'"GetAdsByAdGroupId"', 'User-Agent': 'BingAdsSDKPython 13.0.12 (3, 7, 9)'}
DEBUG:suds.client:Reply HTTP status - 200
b'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode><faultstring xml:lang="en-US">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter https://bingads.microsoft.com/CampaignManagement/v13:AdTypes. The InnerException message was \'Error in line 1 position 2541. Expecting state \'Element\'.. Encountered \'Text\'  with name \'\', namespace \'\'.\'.  Please see InnerException for more details.</faultstring></s:Fault></s:Body></s:Envelope>'
WARNING:suds.client:Web service reported a SOAP processing fault using an unexpected HTTP status code 200. Reporting as an internal server error.

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\bingads\service_client.py", line 323, in __call__
    raise ex
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\bingads\service_client.py", line 315, in __call__
    response = self.service_client.soap_client.service.__getattr__(self.name)(*args, **kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\suds\client.py", line 566, in __call__
    return client.invoke(args, kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\suds\client.py", line 708, in invoke
    result = self.send(soapenv, timeout=timeout)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\suds\client.py", line 756, in send
    return self.process_reply(reply.message, None, None)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\suds\client.py", line 817, in process_reply
    raise WebFault(fault, replyroot)
suds.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/CampaignManagement/v13:AdTypes. The InnerException message was 

**'Error in line 1 position 2541. Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''.'.  Please see InnerException for more details.'**

I suspect that I'm not passing the adtypes correctly, could I get some help as to how I can pass the adtypes into the GetAdsByAdGroupId() function to build the request body.

Thanks!

qitia commented 2 years ago

This should work. let us know otherwise.

        adTypes=campaign_service.factory.create('ArrayOfAdType')
        adTypes.AdType.append('Text')
        adTypes.AdType.append('ResponsiveSearch')

        response=campaign_service.GetAdsByAdGroupId(
            AdGroupId=ad_group_ids['long'][0],
            AdTypes=adTypes
        )
rishaab commented 2 years ago

This worked fine, thanks!

nbro10 commented 6 months ago

@qitia Couldn't the Python library be changed to allow us to pass just the list of types? The current solution seems to be overly complicated and it's not documented anywhere, except here.