Export (0) Print
Expand All

Add Keywords in Perl

The following example shows how to add keywords within an ad group using the Campaign Management AddKeywords 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 commented-out SOAP::Lite statement.
use SOAP::Lite ( +trace => "all", maptype => {} );

use strict;
eval
{
# 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 Microsoft Bing Ads API namespace.
my $xmlns = "https://adcenter.microsoft.com/v8";

# The common data-type namespace.
my $namespaceArrays = 
    "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

# The proxy for the Campaign Management Web service.
my $campaignPROXY =
    $URI."/CampaignManagement/CampaignManagementService.svc?wsdl";

# The service operation that will be called.
my $action = 'AddKeywords';

# 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>;
my $campaignId = <yourcampaignid>;
my $adGroupId = <youradgroupid>; 

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


my @Keywords = SOAP::Data->name(Keywords => \SOAP::Data->value
(
    SOAP::Data->name(Keyword => \SOAP::Data->value
    (
        SOAP::Data->name(BroadMatchBid => \SOAP::Data->value
        (
            SOAP::Data->name(Amount => 0.50)
        )),

        SOAP::Data->name(Param1 => ''),
        SOAP::Data->name(Param2 => ''),
        SOAP::Data->name(Param3 => ''),
        SOAP::Data->name(Text => 'mittens')
    )),
    SOAP::Data->name(Keyword => \SOAP::Data->value
    (
        SOAP::Data->name(ExactMatchBid => \SOAP::Data->value
        (
            SOAP::Data->name(Amount => 12.50)
        )),
        SOAP::Data->name(Param1 => ''),
        SOAP::Data->name(Param2 => ''),
        SOAP::Data->name(Param3 => ''),
        SOAP::Data->name(Text => 'gloves')
    ))
))->attr({'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'});

my @params = 
(
    @header,
    SOAP::Data->name("AdGroupId" => $adGroupId),
    @Keywords
);  

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

my $response = 
    $campaignManagementService->call
    (
        $method => @params
    );

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

    # Display any generic errors.
    my @standardErrors = $response->valueof
    (
        '//AdApiFaultDetail/Errors/AdApiError'
    );
    foreach my $standardError (@standardErrors)
    {
        print "Standard error ($standardError->{Code}) encountered. ";
        print "$standardError->{Message}\n";
    }
    
    # Display any operation errors.
    my @operationErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/OperationErrors/OperationError'
    );
    foreach my $operationError (@operationErrors)
    {
        print "Operation error ($operationError->{Code}) encountered. ";
        print "$operationError->{Message}\n";
    }
    
    # Display any editorial errors.
    my @editorialErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/EditorialErrors/EditorialError'
    );
    foreach my $editorialError (@editorialErrors)
    {
        print "Operation error ($editorialError->{Code}) encountered. ";
        print "$editorialError->{Message}\n";
        print "$editorialError->{DisapprovedText}\n";
    }
    
    # Display any batch errors.
    my @batchErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/BatchErrors/BatchError'
    );
    foreach my $batchError (@batchErrors)
    {
        print "Campaign index $batchError->{Index}\n";
        print "Batch error ($batchError->{Code}) encountered.\n";
        print "$batchError->{Message}\n";
        print "$batchError->{Details}\n";
    }    
}
else 
{
    print "$action succeeded.\n";
    
    # Display the tracking identifier.
    my $trackingId = $response->valueof
    (
        '//TrackingId'
    );
    print "TrackingId: $trackingId\n";
  
    # Display the keyword identifiers.
    my @ids = $response->valueof
    (
        '//KeywordIds/long'
    );
    
    print "The following keyword identifiers were returned by $action:\n";
    foreach my $longval (@ids) 
    {
        print "Keyword ID: $longval\n";
    }   
}

sub CreateHeaders
{
    # The 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 identifier.
    my $CustomerAccountId =SOAP::Header->name
    (
        "CustomerAccountId"=>$customeraccountid
    )->attr({xmlns => $xmlns});
    
    # Initialize the customer identifier.
    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:
© 2014 Microsoft