Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Windows Live
 Hello Microsoft adCenter Sample in ...
Windows Live
Hello Microsoft adCenter Sample in Perl (V5.1)

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

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.

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
{
my $accountID = 489; # Application-specific value.

# Used to retrieve adCenter API tracking ID.
my $trackingId;

# Use either the sandbox or production URI.
# This example is for the sandbox URI.
my $URI = "https://sandboxapi.adcenter.microsoft.com/api/advertiser/v5.1";
# 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/api/advertiser/v5";

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

# @ARGV is the array of credentials passed in 
# at the command line at script run time.
#   $ARGV[0] is the user name.
#   $ARGV[1] is the password.
#   $ARGV[2] is the developer token.
unless (@ARGV == 3)
{
    print "Usage: file.pl username password devtoken\n";
    die;
}
my $username = $ARGV[0];
my $password = $ARGV[1];
my $devtoken = $ARGV[2];

my $UserCreds = SOAP::Header->name
(
    "UserCredentials" => 
    {  
        Username=>SOAP::Data->name("Username" => $username),
        Password=>SOAP::Data->name("Password" => $password)
    }
)-> attr({xmlns => $xmlns});

my $AppToken = SOAP::Header->name
(
    "ApplicationToken"=>{Value =>''}
)-> attr({xmlns => $xmlns});

my $DevToken = SOAP::Header->name
(
    "DeveloperToken"=>{Value =>$devtoken}
)-> attr({xmlns => $xmlns});

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

# Specify the parameters.
my @params =
(
    $DevToken,
    $UserCreds,
    $AppToken,
    SOAP::Data->name
    (
        "AccountId" => $accountID
    )-> 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 fault code and the fault string.
    print $response->faultcode, " ", $response->faultstring, "\n";
    
    # Display the adCenter tracking ID.
    $trackingId = $response->valueof
    (
        '//ApiFaultDetail/TrackingId'
    );
    print "TrackingId: $trackingId\n";
    
    # Display adCenter operation errors.
    my @operationErrors =
        $response->valueof
        (
            '//ApiFaultDetail/OperationErrors/OperationError'
        );
    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
    (
        '//ApiCallTrackingData/TrackingId'
    );
    print "TrackingId: $trackingId\n";

    # Print out 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";
    }
}
};
warn $@ if $@;

When you call your code, pass in the API credentials as command-line parameters. In the code, ARGV represents your API credentials. ARGV[0] is your user name; ARGV[1] is your password; ARGV[2] is your developer token. For example, if your Perl script is named HelloAdCenterV5.pl, execute the script as follows.

HelloAdCenterV5.pl your_user_name your_password your_developer_token

Substitute your API credentials for the placeholders your_user_name, your_password, and your_developer_token.

For more information about calling a Web service that uses the Perl library, see the MSDN topic How to Call a .NET-based Web Service Using the SOAP::Lite Perl Library.

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.

Important:
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.
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker