Microsoft Advertising APIs
Hello Microsoft adCenter Sample in Perl

The following sample shows how to call a Microsoft adCenter API service operation, GetCampaignsByAccountId, in Perl.

Prerequisites

  • You have a Perl development environment set up to use Web services. For example, the following environment works with the adCenter APIs:
    • ActivePerl 5.8.8 – Install from ActiveState. The example code in this topic was developed against the Microsoft Windows distribution of ActivePerl 5.8 – You should be able to use other versions of Perl and other operating systems, although certain parts of the code and configuration may have to be changed.
    • SOAP::Lite 0.71 – This package should come with ActivePerl 5.8.8, but you can also download it from CPAN.
    • Crypt::SSLeay 0.51 – If you are using ActivePerl, you can use PPM to install this with the following command:
      ppm install http://theoryx5.uwinnipeg.ca/ppms/Crypt-SSLeay.ppd
Cc300297.note(en-US,MSADS.60).gifNote:
If this command results in an error stating that PPM cannot connect to theoryx5.uwinnipeg.ca, you may have to set the http_proxy environment variable as discussed in PPM Firewall Problems.

  • You have already received an adCenter API user account, password, and user access key.
  • Your workstation has access to the Internet.

This example assumes that you have already determined your account ID; you must substitute your account ID for the $accountID variable that is assigned 489 in the following code.

Get Campaigns for an Account

package adCenterAPI;

# 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 adCenter API tracking ID.
my $trackingId;

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

# The adCenter API namespace.
my $xmlns = "https://adcenter.microsoft.com/v6";

# 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);

my $username = "username";
my $password = "password";
my $devToken = "devtoken";
my $appToken = "";
my $customerAccountId = "customeraccountid";
my $customerId = "";

# Assemble the service operation parameters.
my @header = CreateV6Header(
    $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 adCenter API call.
my $response =
    $campaignManagementService->call
    (
        $method => @params
    );

# Check for errors.
if ($response->fault) 
{
    print "$action failed.\n";
    
    # Display the adCenter 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 adCenter 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 CreateV6Header
{
    # 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 $@;

In the previous code, substitute your credentials and information for the placeholders username, password, devtoken, customerAccountId, and customerId variables.

For more information about calling a Web service that uses the Perl library, see the MSDN topic How to .

If you want to capture SOAP information for debugging, use the following for the SOAP::Lite statement.

use SOAP::Lite ( +trace => all, maptype => {} );

Notice that the SOAP information contains your API credentials, such as your user name, password, and developer token.

Cc300297.note(en-US,MSADS.60).gifImportant:
Ensure that your SOAP information is secure if you save it to a file. Remove your credentials if you share the SOAP information with other people.

Page view tracker