This topic has not yet been rated - Rate this topic

Retrieve Placement IDs in Python

Microsoft Atlas API

The following Python sample shows how to use the Microsoft Atlas Campaign Management API to retrieve a list of placements that belong to a specified cost package.

This sample was tested by using Python version 2.6.2.

import xml.etree.ElementTree as ET
import httplib

# Declare XML and SOAP-specific namespaces.
NS_SOAP_ENV = "http://schemas.xmlsoap.org/soap/envelope/"
NS_XSI     = "http://www.w3.org/2001/XMLSchema-instance"

# Declare Microsoft Atlas-specific namespaces.
NS_SHARED        = "http://advertising.microsoft.com/v2/Shared/AtlasShared"
NS_DATA_CONTRACT = "http://advertising.microsoft.com/v3/CampaignService/AtlasCampaignManagementService/id"
NS_DATA_OBJECTS  = "http://advertising.microsoft.com/v3/CampaignService/AtlasCampaignManagementService/ml"

# Specify the service operation to perform.
action = "GetPlacementListByCostPackageId"

def GetPlacementsByCostPackage( username,
                                password,
                                devToken,
                                costPackageId ):

    # Programmatically construct the SOAP envelope.
    root = ET.Element("{%s}Envelope" % NS_SOAP_ENV)

    # Create the request headers.
    header = ET.SubElement(root, "{%s}Header"
                           % NS_SOAP_ENV, xmlns=NS_SHARED)
                           
    userCredentials = ET.SubElement(header, "UserCredentials")

    name = ET.SubElement(userCredentials, "UserName")
    name.text = username

    pwd = ET.SubElement(userCredentials, "Password")
    pwd.text = password

    token = ET.SubElement(header, "DeveloperToken")
    token.text = devToken

    # Create and populate the SOAP body.
    body = ET.SubElement(root, "{%s}Body"
                         % NS_SOAP_ENV, xmlns=NS_DATA_CONTRACT)

    request = ET.SubElement(body, action + 'Request')
    costPkg = ET.SubElement(request, "CostPackageId")
    costPkg.text = costPackageId

    # Convert the element tree to a
    # string for the SOAP request.
    soap = ET.tostring(root)

    # Set up the service proxy.
    host   = "atlasapi.atlassolutions.com"
    proxy  = ("https://" + host +
              "/Service/CampaignManagement/v3/CampaignManagementService.svc?wsdl")

    client = httplib.HTTPS(host)
    client.putrequest("POST", proxy)

    # Set the service proxy headers.
    client.putheader("Accept", "text/xml")
    client.putheader("Accept", "multipart/*");
    client.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
    client.putheader("Content-length", "%d" % len(soap))
    client.putheader("SOAPAction", action)
    client.endheaders()

    # Perform the service operation.
    print("Retrieving placements...")
    client.send(soap)

    code, message, responseHeader = client.getreply()

    # Get the response information.
    response = ET.parse(client.getfile())

    if "OK" == message:
        # The service operation was successful.
        placementList = response.findall(".//{%s}Identifier" % NS_DATA_OBJECTS)
       
        for placement in placementList:

            guid = placement.findtext("{%s}Id" % NS_DATA_OBJECTS)
            name = placement.findtext("{%s}Name" % NS_DATA_OBJECTS)

            print ("Placement %s - %s" % (guid, name) )
            
    else:
        # The service operation was not successful.
        
        # Report any operation errors.
        opErrorList = response.findall(".//{%s}OperationError" % NS_SHARED)

        for opError in opErrorList:

            print("\nOperation error encountered.")

            print ("\tError Code: %s"
                   % opError.findtext("{%s}Code" % NS_SHARED) )
            print ("\t   Message: %s"
                   % opError.findtext("{%s}Message" % NS_SHARED) )

        # Report any batch errors.
        batchErrorList = response.findall(".//{%s}BatchError" % NS_SHARED)

        for batchError in batchErrorList:

            print("\nBatch error encountered.")

            print ("\tError Index: %s"
                   % opError.findtext("{%s}Index" % NS_SHARED) )
            print ("\t       Code: %s"
                   % opError.findtext("{%s}Code" % NS_SHARED) )
            print ("\t    Message: %s"
                   % opError.findtext("{%s}Message" % NS_SHARED) )

        # Report any additional error information.
        # These errors are not necessarily related
        # to the Campaign Management API.
        faultcode = response.findtext(".//faultcode")
        if faultcode:
            print ("\nFault code: " + faultcode)

        faultstring = response.findtext(".//faultstring")
        if faultstring:
            print ("Fault string: " + faultstring)

See Also

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.