Export (0) Print
Expand All

Get SOAP Header Elements in Java

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 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 bingads.customermanagement.*;
import bingads.customermanagement.adapi.*;
import bingads.customermanagement.entities.*;
import bingads.customermanagement.exception.*;


/**
 *
 */
public class GetHeaders {

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

    // Specify your credentials.

    private static String UserName = "<UserNameGoesHere>";
    private static String Password = "<PasswordGoesHere>";
    private static String DeveloperToken = "<DeveloperTokenGoesHere>";

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

        try
        {
            locator = new CustomerManagementServiceLocator();
            _namespace = locator.getServiceName().getNamespaceURI();
            _service = (BasicHttpBinding_ICustomerManagementServiceStub) locator.getBasicHttpBinding_ICustomerManagementService();

            // Set the header properties.

            _service.clearHeaders();
            _service.setHeader(_namespace, "DeveloperToken", DeveloperToken);
            _service.setHeader(_namespace, "UserName", UserName);
            _service.setHeader(_namespace, "Password", Password);

            // Given the user credentials provided above, retrieve the customers permitted
            CustomerInfo[] cInfos = GetCustomers("", 100);        
                
            System.out.println("Bing Ads API Header Elements for Current User");
            System.out.println();

            System.out.println("--------------------");
            System.out.println("Required Elements");
            System.out.println("--------------------");
            System.out.println("UserName: " + UserName);
            System.out.println("Password: " + Password);
            System.out.println("DeveloperToken: " + DeveloperToken);
            System.out.println();

            System.out.println("--------------------");
            System.out.println("Elements Required for Some Service Operations");
            System.out.println("--------------------");

            for (CustomerInfo cInfo : cInfos)
            {
                if (cInfo != null)
                {
                    System.out.println("CustomerId: " + cInfo.getId());
                    AccountInfo[] aInfos = GetAccounts(cInfo.getId());

                    // Print a list of all accounts managed by the customer
                    for (AccountInfo aInfo : aInfos)
                    {
                        if (aInfo != null)
                        {
                            System.out.println("CustomerAccountId: " + aInfo.getId());
                        }
                    }
                }
                System.out.println();
            }

            System.out.println("--------------------");
            System.out.println("Elements Not Required and Reserved for Future Use");
            System.out.println("--------------------");
            System.out.println("ApplicationToken");
            System.out.println();
        }
        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 ApiFault )
            {
                ;
            }
            else
            {
                System.out.println("Error encountered: ");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    // Gets the advertising customers that the user
    // has access to and that match the filter criteria.

    static CustomerInfo[] GetCustomers(String filter, int topN) throws RemoteException, Exception
    {
        GetCustomersInfoRequest request = new GetCustomersInfoRequest();
        
        // Set the request information.

        request.setApplicationScope(ApplicationType.Advertiser);
        request.setCustomerNameFilter(filter);
        request.setTopN(topN);

        try
        {
            GetCustomersInfoResponse response = _service.getCustomersInfo(request);

            return response.getCustomersInfo();
        }
        // Customer Management 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);
        }
        // Customer Management service operations can throw ApiFault.
        catch (ApiFault fault)
        {
            // Log this fault.

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

            // 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: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

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

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


    // Gets the accounts info that the customer owns and manages

    static AccountInfo[] GetAccounts(long customerId) throws RemoteException, Exception
    {
        GetAccountsInfoRequest request = new GetAccountsInfoRequest();
        
        // Request only parent accounts such that the returned accounts
        // are within the current customer identifier, and not managed
        // through an agency or reseller. 
        request.setOnlyParentAccounts(true);
        request.setCustomerId(customerId);
        
        try
        {
            return _service.getAccountsInfo(request).getAccountsInfo();
        }
        // Customer Management 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);
        }
        // Customer Management service operations can throw ApiFault.
        catch (ApiFault fault)
        {
            // Log this fault.

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

            // 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: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());

                switch (error.getCode())
                {
                    case 0:     // InternalError
                        break;
                    case 106:   // UserIsNotAuthorized
                        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:
© 2014 Microsoft