Export (0) Print
Expand All

Optimize Bids in Java

This example shows how to get the bid opportunities which have not expired for the specified account using the following Optimizer 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.


import java.rmi.*;
import java.util.List;

import bingads.optimizer.*;
import bingads.optimizer.adapi.*;
import bingads.optimizer.entities.*;
import bingads.optimizer.enums.*;
import bingads.optimizer.errors.*;

/**
 *
 */
public class OptimizeBids{

    private static String namespace = null;
    private static BasicHttpBinding_IOptimizerServiceStub service = null;

    // Specify your credentials.

    private static String UserName = "<UserNameGoesHere>";
    private static String Password = "<PasswordGoesHere>";
    private static String DeveloperToken = "<DeveloperTokenGoesHere>";
    private static long AccountId = <AccountIdGoesHere>;
    private static long CampaignId = <CampaignIdGoesHere>;
    private static long AdGroupId = <AdGroupIdGoesHere>;

    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        OptimizerServiceLocator locator = null;
        

        try
        {
            locator = new OptimizerServiceLocator();
            namespace = locator.getServiceName().getNamespaceURI();
            service = (BasicHttpBinding_IOptimizerServiceStub) locator.getBasicHttpBinding_IOptimizerService();

            // Get the bid opportunities which have not expired for the specified account.

            BidOpportunity[] opportunities = GetBidOpportunities(AccountId, AdGroupId, CampaignId);

            if (opportunities.length == 0) 
            {
                System.out.println("There are no opportunities which have not yet expired for the specified account.");
            }
            else
            {
                String[] opportunityKeys = new String[opportunities.length];

                // Add the first 10,000 opportunity keys to an array
                for (int count=0; count < opportunities.length; count++)
                {
                    if (opportunities[count] != null)
                    {
                        System.out.printf("OpportunityKey: %s", opportunities[count].getOpportunityKey());
                        opportunityKeys[count] = opportunities[count].getOpportunityKey();
                    }
                }

                // Apply the suggested bid opportunities.
                ApplyOpportunities(AccountId, opportunityKeys);
            }
        }
        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 ApiFaultDetail )
            {
                ;
            }
            else
            {
                System.out.println("Error encountered: ");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    // Gets the bid opportunities which have not expired for the specified account.

    public static BidOpportunity[] GetBidOpportunities(long accountId, long adgroupId, long campaignId) throws RemoteException, Exception
    {
        GetBidOpportunitiesRequest request = new GetBidOpportunitiesRequest();
        BidOpportunity[] opportunities = null;

        // Set the header properties.

        service.clearHeaders();
        service.setHeader(namespace, "DeveloperToken", DeveloperToken);
        service.setHeader(namespace, "UserName", UserName);
        service.setHeader(namespace, "Password", Password);
        service.setHeader(namespace, "CustomerAccountId", AccountId);

        // Specify request information.

        request.setAccountId(accountId);
        request.setAdGroupId(adgroupId);
        request.setCampaignId(campaignId);

        try
        {
            return service.getBidOpportunities(request).getOpportunities; 
        }
        // Optimizer service operations can throw AdApiFaultDetail.
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

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

            // If the AdApiError array is not null, the following are examples of error codes that may be found.
	    for (AdApiError error : fault.getErrors())
            {
                System.out.printf("AdApiError\n");
                System.out.printf("Code: %d\nError Code: %s\nMessage: %s\n\n", error.getCode(), error.getErrorCode(), error.getMessage());

                switch (error.getCode())
                {
                    case 0:     // InternalError
                        break;
                    case 105:   // InvalidCredentials
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

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

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

            // If the BatchError array is not null, the following are examples of error codes that may be found.
            for (BatchError error : fault.getBatchErrors())
            {
                System.out.printf("BatchError at Index: %d\n", error.getIndex());
                System.out.printf("Code: %s\nMessage: %s\n\n", error.getCode(), error.getMessage());

                switch (error.getCode().toString())
                {
                    case "InternalError":
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

            // If the OperationError array is not null, the following are examples of error codes that may be found.
            for (OperationError error : fault.getOperationErrors())
            {
                System.out.printf("OperationError\n");
                System.out.printf("Code: %s\nMessage: %s\n\n", error.getCode(), error.getMessage());

                switch (error.getCode().toString())
                {
                    case "InternalError":
                        break;
                    case "UserIsNotAuthorized":
                        break;
                    case "CampaignServiceInvalidAccountId":
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

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

    // Apply opportunties for the specified account.

    public static void ApplyOpportunities(long accountId, String[] opportunityKeys) throws RemoteException, Exception
    {
        ApplyOpportunitiesRequest request = new ApplyOpportunitiesRequest();
        
        // Set the header properties.

        service.clearHeaders();
        service.setHeader(namespace, "DeveloperToken", DeveloperToken);
        service.setHeader(namespace, "UserName", UserName);
        service.setHeader(namespace, "Password", Password);
        service.setHeader(namespace, "CustomerAccountId", AccountId);

        // Specify request information.

        request.setAccountId(accountId);
        request.setOpportunityKeys(opportunityKeys);

        try
        {
            service.applyOpportunities(request);
        }
        // Optimizer service operations can throw AdApiFaultDetail.
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

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

            // If the AdApiError array is not null, the following are examples of error codes that may be found.
	    for (AdApiError error : fault.getErrors())
            {
                System.out.printf("AdApiError\n");
                System.out.printf("Code: %d\nError Code: %s\nMessage: %s\n\n", error.getCode(), error.getErrorCode(), error.getMessage());

                switch (error.getCode())
                {
                    case 0:     // InternalError
                        break;
                    case 105:   // InvalidCredentials
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

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

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

            // If the BatchError array is not null, the following are examples of error codes that may be found.
            for (BatchError error : fault.getBatchErrors())
            {
                System.out.printf("BatchError at Index: %d\n", error.getIndex());
                System.out.printf("Code: %s\nMessage: %s\n\n", error.getCode(), error.getMessage());

                switch (error.getCode().toString())
                {
                    case "InternalError":
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

            // If the OperationError array is not null, the following are examples of error codes that may be found.
            for (OperationError error : fault.getOperationErrors())
            {
                System.out.printf("OperationError\n");
                System.out.printf("Code: %s\nMessage: %s\n\n", error.getCode(), error.getMessage());

                switch (error.getCode().toString())
                {
                    case "InternalError":
                        break;
                    case "UserIsNotAuthorized":
                        break;
                    case "CampaignServiceInvalidAccountId":
                        break;
                    default:
                        System.out.println("Please see MSDN documentation for more details about the error code output above.");
                        break;
                }
            }

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

Community Additions

Show:
© 2015 Microsoft