Search code examples
pythonsudsbing-ads-api

Bingads SDK Python Suds sending wrong envelope


Using BingAds SDK for Python I am not able to perform any operation to update because of a bug that I cannot resolve. The SDK uses Suds for the handling of SOAP operation.

Here is the wsdl: https://campaign.api.bingads.microsoft.com/Api/Advertiser/CampaignManagement/v13/CampaignManagementService.svc?singleWsdl

# This function internally configures the authorization for BingAdsAPI
campaign_service = bc.get_bing_ads_client(account=account, service='CampaignManagementService')

update_ad_groups_request = campaign_service.factory.create('UpdateAdGroupsRequest')
update_ad_groups_request.CampaignId = campaign_id

ad_group = campaign_service.factory.create('AdGroup')
ad_group.Id = ad_group_id

bid = campaign_service.factory.create('Bid')

bid.Amount = new_bid
ad_group.CpcBid = bid

update_ad_groups_request.AdGroups = campaign_service.factory.create('ArrayOfAdGroup')

update_ad_groups_request.AdGroups.AdGroup.append(ad_group)

campaign_service.UpdateAdGroups(update_ad_groups_request)

When I send it it fails saying:

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:CampaignId. The InnerException message was 'There was an error deserializing the object of type System.Int64. The value '' cannot be parsed as the type 'Int64'.'. Please see InnerException for more details.'

When I open the soap Envelope:

....</SOAP-ENV:Header><ns1:Body><ns0:UpdateAdGroupsRequest><ns0:CampaignId>
<ns0:CampaignId>377072652</ns0:CampaignId><ns0:AdGroups><ns0:AdGroup><ns0:CpcBid>
<ns0:Amount>0.91</ns0:Amount></ns0:CpcBid><ns0:Id>1256742239729725</ns0:Id>
<ns0:Network/><ns0:PrivacyStatus/><ns0:Status/></ns0:AdGroup></ns0:AdGroups>
</ns0:CampaignId></ns0:UpdateAdGroupsRequest></ns1:Body></SOAP-ENV:Envelope>

Note how the Campaign ID is twice and also it wraps the whole envelope. I tried also directly with Suds and I have the same issue, other functions also have the same issue.

Because moving to parsing the envelopes by myself, I was wondering if someone has had this issue and what could be the reason.


Solution

  • You should not need to explicitly create a 'Request' object i.e., via the SUDS service client you can pass the request parameters directly via the UpdateAdGroups operation. To confirm I started with expanded_text_ads.py and inserted the following snippet immediately after the AddAdGroups example.

    ad_groups=campaign_service.factory.create('ArrayOfAdGroup')
    ad_group=set_elements_to_none(campaign_service.factory.create('AdGroup'))
    ad_group.Id=ad_group_ids['long'][0]
    cpc_bid=campaign_service.factory.create('Bid')
    cpc_bid.Amount=0.11
    ad_group.CpcBid=cpc_bid
    ad_groups.AdGroup.append(ad_group)
    
    output_status_message("-----\nUpdateAdGroups:")
    add_ad_groups_response=campaign_service.UpdateAdGroups(
        CampaignId=campaign_ids['long'][0],
        AdGroups=ad_groups
    )
    output_status_message("PartialErrors:")
    output_array_of_batcherror(add_ad_groups_response.PartialErrors)
    

    I hope this helps! Please let me know if you have any follow up questions.