Export (0) Print
Expand All

Ad Extensions in Java

The following example shows how to add extensions to an account’s ad extension library, get extensions from the library, associate the extensions with a campaign, get the extensions that are associated with a campaign, and determine why an extension failed editorial review using the following Campaign Management service operations.

This example has been developed and run within the environment described in Getting Started Using Java 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.


// Command line usage:
// java AdExtensions
// 

package campaign;

import java.rmi.*;

import bingads.campaignmanagement.*;
import bingads.campaignmanagement.adapi.*;
//import bingads.campaignmanagement.datacontracts.*;

/**
 *
 */
public class AdExtensions {

    private static String _namespace = null;
    private static BasicHttpBinding_ICampaignManagementServiceStub _service = null;

    // API credentials.

    private static String m_password = "<passwordgoeshere>";
    private static String m_username = "<usernamegoeshere>";
    private static String m_token = "<devtokengoeshere>";

    // Advertiser's account ID and customer ID.

    private static long m_accountId = <accountidgoeshere>;
    private static long m_customerId = <customeridgoeshere>;

    // Campaign to add the extensions to.

    private static long m_campaignId = <campaignidgoeshere>;


    /**
     */
    public static void main(String[] args) {
        CampaignManagementServiceLocator locator = null;
        long[] ids = null;

        try
        {
            locator = new CampaignManagementServiceLocator();
            _namespace = locator.getServiceName().getNamespaceURI();
            _service = (BasicHttpBinding_ICampaignManagementServiceStub) locator.getBasicHttpBinding_ICampaignManagementService();

            // Set the header properties.

            _service.clearHeaders();
            _service.setHeader(_namespace, "CustomerId", m_customerId);
            _service.setHeader(_namespace, "CustomerAccountId", m_accountId);
            _service.setHeader(_namespace, "DeveloperToken", m_token);
            _service.setHeader(_namespace, "UserName", m_username);
            _service.setHeader(_namespace, "Password", m_password);

            ids = AddExtensions(m_accountId);
            PrintExtensions(m_accountId, ids);
            AssociateExtensionsWithCampaign(m_accountId, m_campaignId, ids);
            PrintExtensionsAssociatedWithCampaigns(m_accountId, new long[] { m_campaignId });
        }
        catch (RemoteException e)
        {
            System.out.println("Service communication error encountered: ");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        catch (Exception e)
        {
            // Ignore fault exceptions that we already caught.

            if ( e.getCause() instanceof AdApiFaultDetail ||
                 e.getCause() instanceof EditorialApiFaultDetail ||
                 e.getCause() instanceof ApiFaultDetail )
            {
                ;
            }
            else
            {
                System.out.println("Error encountered: ");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    // Adds an ad extension of each type (for example, a location ad
    // extension and a call ad extension) to the account's
    // ad extension library.

    static long[] AddExtensions(long accountId) throws RemoteException, Exception
    {
        AddAdExtensionsRequest request = new AddAdExtensionsRequest();
        AddAdExtensionsResponse response = null;
        long[] ids = null;

        // Specify the extensions.

        AdExtension2[] extensions = new AdExtension2[3];

        extensions[0] = new SiteLinksAdExtension();
        SiteLink[] siteLinks = new SiteLink[1];
        siteLinks[0] = new SiteLink();
        siteLinks[0].setDestinationUrl("AplineSkiHouse.com/WinterGloveSale");
        siteLinks[0].setDisplayText("Winter Glove Sale");
        ((SiteLinksAdExtension)extensions[0]).setSiteLinks(siteLinks);

        extensions[1] = new CallAdExtension();
        ((CallAdExtension)extensions[1]).setCountryCode("US");
        ((CallAdExtension)extensions[1]).setPhoneNumber("2065550100");
        ((CallAdExtension)extensions[1]).setIsCallOnly(false);

        extensions[2] = new LocationAdExtension();
        ((LocationAdExtension)extensions[2]).setPhoneNumber("206-555-0100");
        ((LocationAdExtension)extensions[2]).setCompanyName("Alpine Ski House");
        ((LocationAdExtension)extensions[2]).setIconMediaId(null); // Use default map icon
        ((LocationAdExtension)extensions[2]).setImageMediaId(null);
        Address address = new Address();
        address.setStreetAddress("1234 Washington Place");
        address.setStreetAddress2("Suite 1210");
        address.setCityName("Woodinville");
        address.setProvinceName("WA"); // Can contain the state name or code (i.e. WA)
        address.setCountryCode("US");
        address.setPostalCode("98608");
        ((LocationAdExtension)extensions[2]).setAddress(address);

        // Set the request information.

        request.setAccountId(accountId);
        request.setAdExtensions(extensions);

        try
        {
            response = _service.addAdExtensions(request);

            int idCount = response.getAdExtensionIdentities().length;
            ids = new long[idCount];

            for (int i = 0; i < idCount; i++)
            {
                ids[i] = response.getAdExtensionIdentities()[i].getId();
            }
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("AddAdExtensions failed with the following faults:\n");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid " +
                        "or the account is inactive.\n");
                }
                else
                {
                    System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                        error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetail());
                }
            }

            throw new Exception("", fault);
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("AddAdExtensions failed with the following faults:\n");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to call this operation.\n");
                        break;

                    case 1030: //  CampaignServiceAccountIdHasToBeSpecified
                        System.out.println("The CustomerAccountId header element " +
                            "cannot be null or empty.\n");
                        break;

                    case 3862: // CampaignServiceLocationAdExtensionsEntityLimitExceeded
                        System.out.println("The number of location ad extensions in the " +
                            "ad extensions list exceeds the maximum allowed.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            // This list of errors codes is just a sampling of the possible errors that
            // AddAdExtensions can throw.

            // Note that the Details element of BatchErrors contains the zero-based
            // index of the site link in the list of site links that caused the error.

            for (BatchError error : fault.getBatchErrors())
            {
                switch (error.getCode())
                {
                    case 3804: // CampaignServiceSiteLinkDestinationUrlNullOrEmpty
                        System.out.printf("The destination URL of site link #%s in " +
                            "extension #%d cannot be null or empty.\n\n",
                            error.getDetails(),
                            error.getIndex());
                        break;

                    case 3805: // CampaignServiceSiteLinkDisplayTextNullOrEmpty
                        System.out.printf("The display URL of site link #%s in " +
                            "extension #%d cannot be null or empty.\n\n",
                            error.getDetails(),
                            error.getIndex());
                        break;

                    case 3806: // CampaignServiceSiteLinkDestinationUrlTooLong
                        System.out.printf("The destination URL of site link #%s in " +
                            "extension #%d is too long.\n\n",
                            error.getDetails(),
                            error.getIndex());
                        break;

                    case 3807: // CampaignServiceSiteLinkDisplayTextTooLong
                        System.out.printf("The display URL of site link #%s in " +
                            "extension #%d is too long.\n\n",
                            error.getDetails(),
                            error.getIndex());
                        break;

                    case 3809: // CampaignServiceTooManySiteLinks
                        System.out.println("The list of site links contains too many items.\n");
                        break;

                    case 3828: // CampaignServiceAdExtensionAddressIsNull
                        System.out.printf("The address in extension #%d cannot be null.\n\n",
                            error.getIndex());
                        break;

                    case 3829: // CampaignServiceAdExtensionStreetAddressNullOrEmpty
                        System.out.printf("The first line of the street address in " +
                            "extension #%d cannot be null or empty.\n\n",
                            error.getIndex());
                        break;

                    case 3830: // CampaignServiceAdExtensionStreetAddressTooLong
                        System.out.printf("The first line of the street address in " +
                            "extension #%d is too long.\n\n",
                            error.getIndex());
                        break;

                    case 3834: // CampaignServiceAdExtensionCityNameNullOrEmpty
                        System.out.printf("The city name in extension #%d cannot be " +
                            "null or empty.\n\n",
                            error.getIndex());
                        break;

                    case 3839: // CampaignServiceAdExtensionPostalCodeNullOrEmpty
                        System.out.printf("The postal code in extension #%d cannot " +
                            "be null or empty.\n\n",
                            error.getIndex());
                        break;

                    case 3842: // CampaignServiceAdExtensionCountryCodeNullOrEmpty
                        System.out.printf("The country code in extension #%d cannot " +
                            "be null or empty.\n\n",
                            error.getIndex());
                        break;

                    case 3844: // CampaignServiceAdExtensionCountryCodeInvalid
                        System.out.printf("The country code in extension #%d is not valid.\n\n",
                            error.getIndex());
                        break;

                    case 3847: // CampaignServiceAdExtensionCompanyNameNullOrEmpty
                        System.out.printf("The compangy name in extension #%d cannot " +
                            "be null or empty.\n\n",
                            error.getIndex());
                        break;

                    case 3850: // CampaignServiceAdExtensionPhoneNumberTooLong
                        System.out.printf("The phone number in extension #%d is too long.\n\n",
                            error.getIndex());
                        break;

                    case 3851: // CampaignServiceAdExtensionPhoneNumberInvalid
                        System.out.printf("The phone number in extension #%d is not valid.\n\n",
                            error.getIndex());
                        break;

                    case 3852: // CampaignServiceAdExtensionIconMediaIdInvalid
                        System.out.printf("The icon in extension #%d was not " +
                            "found in the account’s media library.\n\n",
                            error.getIndex());
                        break;

                    case 3853: // CampaignServiceAdExtensionIconTooLarge
                        System.out.printf("The width and height of the icon in " +
                            "extension #%d is greater than the maximum allowed.\n\n",
                            error.getIndex());
                        break;

                    case 3854: // CampaignServiceAdExtensionImageMediaIdInvalid
                        System.out.printf("The image in extension #%d was not found " +
                            "in the account’s media library.\n\n",
                            error.getIndex());
                        break;

                    case 3855: // CampaignServiceAdExtensionImageTooLarge
                        System.out.printf("The width and height of the image in " +
                            "extension #%d is greater than the maximum allowed.\n\n",
                            error.getIndex());
                        break;

                    case 3856: //  CampaignServiceLocationAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To add location extensions, the customer must be a " +
                            "member of the location ad extensions v2 pilot program.\n\n");
                        break;

                    case 3857: //  CampaignServiceCallAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To add call extensions, the customer must be a " +
                            "member of the call ad extensions v2 pilot program.\n\n");
                        break;

                    case 3859: // CampaignServiceAdExtensionProvinceCodeInvalid
                        System.out.printf("The province code in extension #%d " +
                            "is not valid.\n\n",
                            error.getIndex());
                        break;

                    case 3860: // CampaignServiceAdExtensionProvinceCodeRequiredIfProvinceNameEmpty
                        System.out.printf("The province code in extension #%d is " +
                            "required if the province name is empty.\n\n",
                            error.getIndex());
                        break;

                    case 3861: // CampaignServiceAdExtensionPhoneNumberNullOrEmpty
                        System.out.printf("The phone number in extension #%d " +
                            "cannot be null or empty.\n\n",
                            error.getIndex());
                        break;

                    default:
                        System.out.printf("Unable to add extension #%d\n", error.getIndex());
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            throw new Exception("", fault);
        }

        return ids;
    }

    // Gets the specified ad extensions from the account's library
    // and prints their contents.

    static void PrintExtensions(long accountId, long[] ids) throws RemoteException, Exception
    {
        GetAdExtensionsByIdsRequest request = new GetAdExtensionsByIdsRequest();
        GetAdExtensionsByIdsResponse response = null;
        int i = 0;

        // Set the request information.

        request.setAccountId(accountId);
        request.setAdExtensionIds(ids);
        request.setAdExtensionType(new String[] {
            AdExtensionsTypeFilterNull._SiteLinksAdExtension,
            AdExtensionsTypeFilterNull._CallAdExtension,
            AdExtensionsTypeFilterNull._LocationAdExtension
        });

        try
        {
            response = _service.getAdExtensionsByIds(request);

            for (AdExtension2 extension : response.getAdExtensions())
            {
                if (null == extension)
                {
                    System.out.printf("Extension ID %d is not a valid ID or the extension " +
                        "does not match the request type.\n\n",
                        ids[i]);
                }
                else
                {
                    System.out.println("Ad extension ID: " + extension.getId());
                    System.out.println("Ad extension Type: " + extension.getType());

                    if (extension instanceof SiteLinksAdExtension)
                    {
                        for (SiteLink siteLink : ((SiteLinksAdExtension)extension).getSiteLinks())
                        {
                            System.out.println("  Display URL: " + siteLink.getDisplayText());
                            System.out.println("  Destination URL: " + siteLink.getDestinationUrl());
                            System.out.println();
                        }
                    }
                    else if (extension instanceof CallAdExtension)
                    {
                        System.out.println("Phone number: " + ((CallAdExtension)extension).getPhoneNumber());
                        System.out.println("Country: " + ((CallAdExtension)extension).getCountryCode());
                        System.out.println("Is only clickable item: " + ((CallAdExtension)extension).getIsCallOnly());
                        System.out.println();
                    }
                    else if (extension instanceof LocationAdExtension)
                    {
                        // Ads will not include the location extension until the business' coordinates 
                        // (longitude and latitude) are determined using the business' address. The GeoCodeStatus
                        // field indicates the progress. It can take from seconds to minutes to determine
                        // the coordinates.

                        System.out.println("Company name: " + ((LocationAdExtension)extension).getCompanyName());
                        System.out.println("Phone number: " + ((LocationAdExtension)extension).getPhoneNumber());
                        System.out.println("Street: " + ((LocationAdExtension)extension).getAddress().getStreetAddress());
                        System.out.println("City: " + ((LocationAdExtension)extension).getAddress().getCityName());
                        System.out.println("State: " + ((LocationAdExtension)extension).getAddress().getProvinceName());
                        System.out.println("Country: " + ((LocationAdExtension)extension).getAddress().getCountryCode());
                        System.out.println("Zip code: " + ((LocationAdExtension)extension).getAddress().getPostalCode());
                        System.out.println("Business coordinates determined?: " + ((LocationAdExtension)extension).getGeoCodeStatus());
                        System.out.println("Map icon ID: " + ((LocationAdExtension)extension).getIconMediaId());
                        System.out.println("Business image ID: " + ((LocationAdExtension)extension).getImageMediaId());
                        System.out.println();
                    }
                    else
                    {
                        System.out.println("  Unknown extension type");
                    }
                }

                System.out.println();
                i++;
            }
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsByIds failed with the following faults:\n");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid " +
                        "or the account is inactive.\n");
                }
                else
                {
                    System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                        error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetail());
                }
            }

            throw new Exception("", fault);
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsByIds failed with the following faults:\n");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to call this operation.\n");
                        break;

                    case 1030: //  CampaignServiceAccountIdHasToBeSpecified
                        System.out.println("The CustomerAccountId header element " +
                            "cannot be null or empty.\n");
                        break;

                    case 1102: //  CampaignServiceInvalidAccountId
                        System.out.println("The account ID is not valid.\n");
                        break;

                    case 3814: //  CampaignServiceInvalidAdExtensionTypeFilter
                        System.out.println("The ad extension type filter is null " +
                            "or is not one of the allowed extension types.\n");
                        break;

                    case 3815: //  CampaignServiceAdExtensionIdArrayShouldNotBeNullOrEmpty
                        System.out.println("The list of extension IDs cannot be null or empty.\n");
                        break;

                    case 3816: //  CampaignServiceAdExtensionIdsArrayExceedsLimit
                        System.out.println("The list of ad extension IDs is too long.\n");
                        break;

                    case 3856: //  CampaignServiceLocationAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To get location extensions, the customer must " +
                            "be a member of the location ad extensions v2 pilot program.\n");
                        break;

                    case 3857: //  CampaignServiceCallAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To get call extensions, the customer must be a " +
                            "member of the call ad extensions v2 pilot program.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            for (BatchError error : fault.getBatchErrors())
            {
                switch (error.getCode())
                {
                    case 3817: //  CampaignServiceDuplicateInAdExtensionIds
                        System.out.printf("The list of extension IDs contains duplicates. " +
                            "Id %d is a duplicate.\n\n",
                            request.getAdExtensionIds()[error.getIndex()]);
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nIndex: %d\nMessage: %s\nDetail: %s\n",
                                error.getErrorCode(), error.getCode(), error.getIndex(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            throw new Exception("", fault);
        }
    }

    // Associates one or more extensions with a campaign.

    static void AssociateExtensionsWithCampaign(long accountId, long campaignId, long[] extensionIds) throws RemoteException, Exception
    {
        SetAdExtensionsToCampaignsRequest request = new SetAdExtensionsToCampaignsRequest();
        SetAdExtensionsToCampaignsResponse response = null;

        if (0 == extensionIds.length)
        {
            return;
        }

        // Loop through the list of extension IDs and build
        // the association objects.

        AdExtensionIdToCampaignIdAssociation[] associations = new AdExtensionIdToCampaignIdAssociation[extensionIds.length];

        for (int i = 0; i < extensionIds.length; i++)
        {
            associations[i] = new AdExtensionIdToCampaignIdAssociation();
            associations[i].setAdExtensionId(extensionIds[i]);
            associations[i].setCampaignId(campaignId);
        }

        // Set the request information.

        request.setAccountId(accountId);
        request.setAdExtensionIdToCampaignIdAssociations(associations);

        try
        {
            response = _service.setAdExtensionsToCampaigns(request);
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("SetAdExtensionsToCampaigns failed with the following faults:\n");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid or " +
                        "the account is inactive.\n");
                }
                else
                {
                    System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n",
                        error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetail());
                }
            }

            throw new Exception("", fault);
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("SetAdExtensionsToCampaigns failed with the following faults:\n");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to call " +
                            "this operation.\n");
                        break;

                    case 1030: //  CampaignServiceAccountIdHasToBeSpecified
                        System.out.println("The CustomerAccountId header element " +
                            "cannot be null or empty.\n");
                        break;

                    case 1102: //  CampaignServiceInvalidAccountId
                        System.out.println("The account ID is not valid.\n");
                        break;

                    case 3810: //  CampaignServiceAdExtensionIdToCampaignIdAssociationArrayShouldNotBeN
                        System.out.println("The list of ad extension to campaign " +
                            "associations cannot be null or empty.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            for (BatchError error : fault.getBatchErrors())
            {
                switch (error.getCode())
                {
                    case 1100: //  CampaignServiceInvalidCampaignId
                        System.out.printf("The campaign ID specified in ad extension " +
                            "to campaign association #%d} is not valid.\n\n",
                            error.getIndex());
                        break;

                    case 1397: //  CampaignServiceEditorialGenericError
                        System.out.printf("The ad extension specified in ad extension " +
                            "to campaign association #%d failed editorial review. " +
                            "The term that failed review is \"%s\".\n\n",
                            error.getIndex(),
                            error.getDetails());
                        break;

                    case 3812: //  CampaignServiceInvalidAdExtensionId
                        System.out.printf("The ad extension ID specified in ad " +
                            "extension to campaign association #%d is not valid.\n\n",
                            error.getIndex());
                        break;

                    case 3818: //  CampaignServiceCannotAssignMoreThanOneAdExtensionTypeToAnEntity
                        System.out.printf("The campaign specified in ad extension to " +
                            "campaign association #%d can contain only one extension of " +
                            "the specified type.\n\n",
                            error.getIndex());
                        break;

                    case 3822: //  CampaignServiceSiteLinkAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, you must be a " +
                            "member of the site links ad extensions pilot program.\n\n");
                        break;

                    case 3825: //  CampaignServiceDuplicateInAdExtensionIdToCampaignIdAssociations
                        System.out.printf("The list of ad extension to campaign " +
                            "associations contains a duplicate ad extension ID " +
                            "for the same campaign. Association #%d contains the duplicate.\n\n",
                            error.getIndex());
                        break;

                    case 3856: //  CampaignServiceLocationAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the location ad extensions pilot program.\n");
                        break;

                    case 3857: //  CampaignServiceCallAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the call ad extensions pilot program.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nIndex: %d\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getIndex(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            throw new Exception("", fault);
        }
    }

    // Gets the extensions associated with the specified campaigns
    // and print the extensions ID, type, and editorial status.

    private static void PrintExtensionsAssociatedWithCampaigns(long accountId, long[] campaignIds) throws RemoteException, Exception
    {
        GetAdExtensionsByCampaignIdsRequest request = new GetAdExtensionsByCampaignIdsRequest();
        GetAdExtensionsByCampaignIdsResponse response = null;
        AdExtensionEditorialReasonCollection[] editorialCollection = null;
        int i = 0;

        // Set the request information.

        request.setAccountId(accountId);
        request.setCampaignIds(campaignIds);
        request.setAdExtensionType(new String[] {
            AdExtensionsTypeFilterNull._SiteLinksAdExtension,
            AdExtensionsTypeFilterNull._CallAdExtension,
            AdExtensionsTypeFilterNull._LocationAdExtension
        });

        try
        {
            response = _service.getAdExtensionsByCampaignIds(request);

            // Loop through the results for each campaign.

            for (CampaignAdExtensionCollection campaign : response.getCampaignAdExtensionCollection())
            {
                if (null == campaign)
                {
                    System.out.printf("Campaign ID %d is not valid.\n", campaignIds[i]);
                }
                else
                {
                    // If the campaign contains extensions of the specified type...

                    if (campaign.getCampaignAdExtensions().length > 0)
                    {
                        System.out.printf("Ad extensions associated with campaign ID %d:\n", campaign.getCampaignAdExtensions()[0].getCampaignId());

                        for (CampaignAdExtension extension : campaign.getCampaignAdExtensions())
                        {
                            System.out.println("Ad extension ID: " + extension.getAdExtension().getId());
                            System.out.println("Ad extension Type: " + extension.getAdExtension().getType());
                            System.out.printf("Ad extension Editorial Status: %s\n",
                                (extension.getEditorialStatus() == CampaignAdExtensionEditorialStatus.Inactive) ?
                                    "Under review" : extension.getEditorialStatus().toString()
                                );

                            // If the extension failed editorial review, find out why and which
                            // publisher countries were affected.

                            if (extension.getEditorialStatus() == CampaignAdExtensionEditorialStatus.ActiveLimited ||
                                extension.getEditorialStatus() == CampaignAdExtensionEditorialStatus.Disapproved)
                            {
                                editorialCollection = GetEditorialReasons(
                                    accountId,
                                    campaign.getCampaignAdExtensions()[0].getCampaignId(),
                                    (long)extension.getAdExtension().getId(),
                                    extension.getAdExtension().getType());

                                for (AdExtensionEditorialReason editorialReason : editorialCollection[0].getReasons())
                                {
                                    System.out.println("  Reason code: " + editorialReason.getReasonCode());
                                    System.out.println("  Term : " + editorialReason.getTerm());
                                    System.out.println("  Location : " + editorialReason.getLocation());
                                    System.out.print("  Publisher countries: ");

                                    int j = 1;
                                    int length = editorialReason.getPublisherCountries().length;
                                    for (String country : editorialReason.getPublisherCountries())
                                    {
                                        System.out.printf("%s%s", country, (j++ < length) ? ", " : "\n");
                                    }
                                }
                            }

                            System.out.println();
                        }
                    }
                    else
                    {
                        System.out.printf("Campaign ID %d does not contain ad extensions " +
                            "that match the specified extension types.",
                            campaignIds[i]);
                    }
                }

                i++;
            }
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsByCampaignIds failed with the following faults:\n");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid or " +
                        "the account is inactive.\n");
                }
                else
                {
                    System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                        error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetail());
                }
            }

            throw new Exception("", fault);
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsByCampaignIds failed with the following faults:\n");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to call " +
                            "this operation.\n");
                        break;

                    case 1030: //  CampaignServiceAccountIdHasToBeSpecified
                        System.out.println("The CustomerAccountId header element " +
                            "cannot be null or empty.\n");
                        break;

                    case 1102: //  CampaignServiceInvalidAccountId
                        System.out.println("The account ID is not valid.\n");
                        break;

                    case 1118: //  CampaignServiceCampaignIdsArrayShouldNotBeNullOrEmpty
                        System.out.println("The list of campaign IDs cannot " +
                            "be null or empty.\n");
                        break;

                    case 1119: //  CampaignServiceCampaignIdsArrayExceedsLimit
                        System.out.println("The list of campaign IDs is too long.\n");
                        break;

                    case 3814: //  CampaignServiceInvalidAdExtensionTypeFilter
                        System.out.println("The ad extension type filter is null " +
                            "or is not one of the allowed extension types.\n");
                        break;

                    case 3856: //  CampaignServiceLocationAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the location ad extensions v2 pilot program.\n");
                        break;

                    case 3857: //  CampaignServiceCallAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the call ad extensions v2 pilot program.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            for (BatchError error : fault.getBatchErrors())
            {
                switch (error.getCode())
                {
                    case 1107: //  CampaignServiceDuplicateCampaignIds
                        System.out.printf("The list of campaign IDs contains duplicates. " +
                            "Id %d is a duplicate.\n\n",
                            request.getCampaignIds()[error.getIndex()]);
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nIndex: %d\nMessage: %s\nDetail: %s\n\n",
                                error.getErrorCode(), error.getCode(), error.getIndex(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            throw new Exception("", fault);
        }
    }

    // Gets the reasons why the specified extension failed editorial
    // review. The review is performed in the context of the campaign.

    private static AdExtensionEditorialReasonCollection[] GetEditorialReasons(
        long accountId,
        long campaignId,
        long extensionId,
        String extensionType)  throws RemoteException, Exception
    {
        GetAdExtensionsEditorialReasonsByCampaignIdsRequest request = new GetAdExtensionsEditorialReasonsByCampaignIdsRequest();
        GetAdExtensionsEditorialReasonsByCampaignIdsResponse response = null;

        // Specify the extension to campaign association.

        AdExtensionIdToCampaignIdAssociation[] associations = new AdExtensionIdToCampaignIdAssociation[1];
        associations[0] = new AdExtensionIdToCampaignIdAssociation();
        associations[0].setAdExtensionId(extensionId);
        associations[0].setCampaignId(campaignId);

        // Set the request information.

        request.setAccountId(accountId);
        request.setAdExtensionIdToCampaignIdAssociations(associations);
        request.setAdExtensionType(new String[] {extensionType});

        try
        {
            response = _service.getAdExtensionsEditorialReasonsByCampaignIds(request);
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsEditorialReasonsByCampaignIds failed with the following faults:\n");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid " +
                        "or the account is inactive.\n");
                }
                else
                {
                    System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                        error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetail());
                }
            }

            throw new Exception("", fault);
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("GetAdExtensionsEditorialReasonsByCampaignIds failed with the following faults:\n");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to " +
                            "call this operation.\n");
                        break;

                    case 1029: //  CampaignServiceCustomerIdHasToBeSpecified.
                        System.out.println("The customer ID header element cannot be null or empty.\n");
                        break;

                    case 1030: //  CampaignServiceAccountIdHasToBeSpecified
                        System.out.println("The CustomerAccountId header element " +
                            "cannot be null or empty.\n");
                        break;

                    case 1102: //  CampaignServiceInvalidAccountId
                        System.out.println("The account ID is not valid.\n");
                        break;

                    case 3810: //  CampaignServiceAdExtensionIdToCampaignIdAssociationArrayShouldNotBeNullOrEmpty
                        System.out.println("The list of ad extension to campaign " +
                            "associations cannot be null or empty.\n");
                        break;

                    case 3811: //  CampaignServiceAdExtensionIdToCampaignIdAssociationArrayLimitExceeded
                        System.out.println("The list of ad extension to campaign " +
                            "associations is too long.\n");
                        break;

                    case 3814: //  CampaignServiceInvalidAdExtensionTypeFilter
                        System.out.println("The ad extension type filter is null " +
                            "or is not one of the allowed extension types.\n");
                        break;

                    case 3856: //  CampaignServiceLocationAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the location ad extensions pilot program.\n");
                        break;

                    case 3857: //  CampaignServiceCallAdExtensionPilotNotEnabledForCustomer
                        System.out.println("To call this operation, the customer must be a " +
                            "member of the call ad extensions pilot program.\n");
                        break;

                    default:
                        System.out.printf("Error code: %s (%d)\nMessage: %s\nDetail: %s\n\n",
                            error.getErrorCode(), error.getCode(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            for (BatchError error : fault.getBatchErrors())
            {
                switch (error.getCode())
                {
                    default:
                        System.out.printf("Error code: %s (%d)\nIndex: %d\nMessage: %s\nDetail: %s\n\n",
                                error.getErrorCode(), error.getCode(), error.getIndex(), error.getMessage(), error.getDetails());
                        break;
                }
            }

            throw new Exception("", fault);
        }

        return response.getEditorialReasons();
    }
}

Community Additions

Show:
© 2015 Microsoft