Get Campaigns in Perl

The following example shows how to get campaigns within an account using the Campaign Management GetCampaignsByAccountId service operation.

This example has been developed and run within the environment described in Getting Started Using Perl with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

package BingAdsAPI;

# For release code, use the following SOAP::Lite statement.
#use SOAP::Lite ( +maptype => {} );
# For debugging code, use the following SOAP::Lite statement.
use SOAP::Lite ( +trace => "all", maptype => {} );

use strict;
eval
{
# Used to retrieve API tracking ID.
my $trackingId;

# Use either the sandbox or the production URI.
# This example is for the sandbox URI.
my $URI = "https://api.sandbox.bingads.microsoft.com/api/advertiser/v8";
# The following commented-out line contains the production URI.
#my $URI = "https://adcenterapi.microsoft.com/api/advertiser/v8";

# The Bing Ads API namespace.
my $xmlns = "https://adcenter.microsoft.com/v8";

# The proxy for the campaign management web service.
my $campaignPROXY =
    $URI."/CampaignManagement/CampaignManagementService.svc?wsdl";

# The name of the service operation that will be called.
my $action = 'GetCampaignsByAccountId';

# The web service.
my $campaignManagementService = SOAP::Lite->new(
    uri => $URI, 
    proxy => $campaignPROXY, 
    on_action => ( sub { return $action } ));
$campaignManagementService->autotype(0);
$campaignManagementService->multirefinplace(1);
$campaignManagementService->readable(1);

# Application-specific values
my $username = "<yourusername>";
my $password = "<yourpassword>";
my $devToken = "<yourdevelopertoken>";
my $appToken = "";
my $customerAccountId = <youraccountid>;
my $customerId = <yourcustomerid>;

# Assemble the service operation parameters.
my @header = CreateHeaders(
    $username,
    $password,
    $devToken,
    $appToken,
    $customerAccountId,
    $customerId);

# Specify the parameters.
my @params =
(
    @header,
    SOAP::Data->name
    (
        "AccountId" => $customerAccountId
    )
);

my $method = SOAP::Data->name
(
    $action.'Request'
)-> attr({xmlns => $xmlns});

# Make the API call.
my $response =
    $campaignManagementService->call
    (
        $method => @params
    );

# Check for errors.
if ($response->fault) 
{
    print "$action failed.\n";
    
    # Display the tracking ID.
    my $trackingId = $response->valueof
    (
        '//AdApiFaultDetail/TrackingId'
    );
    print "TrackingId: $trackingId\n";

    # Display any operation errors.
    my @operationErrors = $response->valueof
    (
        '//AdApiFaultDetail/Errors/AdApiError'
    );
    foreach my $operationError (@operationErrors)
    {
        print "Operation error ($operationError->{Code}) encountered. ";
        print "$operationError->{Message}\n";
    }
}
else
{
    print "Successful call to $action.\n";

    # Display the tracking ID.
    $trackingId = $response->valueof
    (
        '//TrackingId'
    );
    print "TrackingId: $trackingId\n";

    # Print the campaign IDs and names.
    my @campaigns = $response->valueof
    (
        '//Campaigns/Campaign'
    );
    print "The following campaigns were returned by $action.\n";
    foreach my $campaign (@campaigns)
    {
        print "CampaignID: $campaign->{Id} ";
        print "CampaignName: $campaign->{Name}\n";
    }
}

sub CreateHeaders
{
    # Method parameters.
    my (
        $username,
        $password,
        $devtoken,
        $apptoken,
        $customeraccountid,
        $customerid) = @_;
    
    # Initialize the application token.
    my $AppToken = SOAP::Header->name
    (
        "ApplicationToken"=>$apptoken
    )->attr({xmlns => $xmlns});
    
    # Initialize the developer token.
    my $DevToken =SOAP::Header->name
    (
        "DeveloperToken"=>$devtoken
    )->attr({xmlns => $xmlns});
    
    # Initialize the user name.
    my $UserName =SOAP::Header->name
    (
        "UserName"=>$username
    )->attr({xmlns => $xmlns});
    
    # Initialize the password.
    my $Password =SOAP::Header->name
    (
        "Password"=>$password
    )->attr({xmlns => $xmlns});
    
    # Initialize the customer account ID.
    my $CustomerAccountId =SOAP::Header->name
    (
        "CustomerAccountId"=>$customeraccountid
    )->attr({xmlns => $xmlns});
    
    # Initialize the customer ID.
    my $CustomerId =SOAP::Header->name
    (
        "CustomerId"=>$customerid
    )->attr({xmlns => $xmlns});
    
    # Assemble the header parameters.
    my @headerParams =
    (
        $AppToken,
        $CustomerAccountId,
        $CustomerId,
        $DevToken,
        $Password,
        $UserName, 
    );
    
    return @headerParams;
}
};
warn $@ if $@;

Community Additions

Show: