Export (0) Print
Expand All

Get SOAP Header Elements in Perl

The following example shows how to get your customer and account identifiers using the following Customer Management service operations.

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;

# Application-specific values
my $username = "<UserNameGoesHere>"
my $password = "<PasswordGoesHere>"
my $devToken = "<DeveloperTokenGoesHere>"
my $appToken = "";

# This example host is for the sandbox environment.
# Update the URI as needed when you use the production environment.
my $URI = "https://sharedservices.api.sandbox.bingads.microsoft.com/Api/CustomerManagement/v8";
# The following is the production URI.
#my $URI = "https://sharedservices.adcenterapi.microsoft.com/Api/CustomerManagement/v8";

# The Bing Ads Customer Management API namespace.
my $xmlns = "https://adcenter.microsoft.com/api/customermanagement";

# The proxy for the customer management web service.
my $customerPROXY =
    $URI."/CustomerManagementService.svc?wsdl";

# The current service operation
my $action = '';

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


# Assemble the service operation parameters.
my @header = CreateHeaders(
    $username,
    $password,
    $devToken,
    $appToken);
    
# Specify the parameters for GetCustomersInfo.
my @getcustomersparams =
(
    @header,
    SOAP::Data->name("CustomerNameFilter" => ""),
    SOAP::Data->name("TopN" => 100),
    SOAP::Data->name("ApplicationScope" => "Advertiser")
);

$action = 'GetCustomersInfo';

my $getcustomersmethod = SOAP::Data->name
(
    $action . 'Request'
)-> attr({xmlns => $xmlns});
    
# Given the user credentials provided above, retrieve the top 100 customers permitted
    
my $response =
    $customerManagementService->call
    (
        $getcustomersmethod => @getcustomersparams
    );

print "Bing Ads API Header Elements for Current User\n\n";
    
print "--------------------\n";
print "Required Elements\n";
print "--------------------\n";
print "UserName: " . $username . "\n";
print "Password: " . $password . "\n";
print "DeveloperToken: " . $devToken . "\n\n";
    
print "--------------------\n";
print "Elements Required for Some Service Operations\n";
print "--------------------\n";

my @cInfos = $response->valueof
(
    '//CustomersInfo/CustomerInfo'
);

foreach my $cInfo (@cInfos)
{
    if ($cInfo)
    {
        print "CustomerId: " . $cInfo->{Id} . "\n";
        
        # Specify the parameters for GetAccountsInfo.
        my @getaccountsparams =
        (
            @header,
            SOAP::Data->name("CustomerId" => $cInfo->{Id}),
            SOAP::Data->name("OnlyParentAccounts" => "true")
        );

        $action = 'GetAccountsInfo';
        my $getaccountsmethod = SOAP::Data->name
        (
            $action . 'Request'
        )-> attr({xmlns => $xmlns});
        
        my $response =
        $customerManagementService->call
        (
            $getaccountsmethod => @getaccountsparams
        );
        
        my @aInfos = $response->valueof
        (
            '//AccountsInfo/AccountInfo'
        );
        
        # Print a list of all accounts managed by the customer
        foreach my $aInfo (@aInfos)
        {
            if ($aInfo)
            {
                print "CustomerAccountId: " . $aInfo->{Id} . "\n";
            }
        }
    }
    print "\n";
}

print "--------------------\n";
print "Elements Not Required and Reserved for Future Use\n";
print "--------------------\n";
print "ApplicationToken\n\n";


# 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";
    }
}

sub CreateHeaders
{
    # Method parameters.
    my (
        $username,
        $password,
        $devtoken,
        $apptoken) = @_;
    
    # 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});
    
    # Assemble the header parameters.
    my @headerParams =
    (
        $AppToken,
        $DevToken,
        $Password,
        $UserName, 
    );
    
    return @headerParams;
}
};
warn $@ if $@;


Community Additions

Show:
© 2014 Microsoft