Export (0) Print
Expand All

Get SOAP Header Elements in C#

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 C# and Visual Basic 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.


using System;
using System.Collections.Generic;
using System.ServiceModel;

using GetHeaders.BingAds.CustomerManagement;

namespace GetHeaders
{
    class Program
    {
        private static CustomerManagementServiceClient _service;

        // Specify your credentials.
        
        private const string UserName = "<UserNameGoesHere>";
        private const string Password = "<PasswordGoesHere>";
        private const string DeveloperToken = "<DeveloperTokenGoesHere>";

        static void Main()
        {
            try
            {
                _service = new CustomerManagementServiceClient("BasicHttpBinding_ICustomerManagementService");

                // Given the user credentials provided above, retrieve up to 100 of the customers permitted.

                IEnumerable<CustomerInfo> cInfos = GetCustomers(String.Empty, 100);

                Console.WriteLine("Bing Ads API Header Elements for Current User");
                Console.WriteLine();

                Console.WriteLine("--------------------");
                Console.WriteLine("Required Elements");
                Console.WriteLine("--------------------");
                Console.WriteLine("UserName: {0}", UserName);
                Console.WriteLine("Password: {0}", Password);
                Console.WriteLine("DeveloperToken: {0}", DeveloperToken);
                Console.WriteLine();

                Console.WriteLine("--------------------");
                Console.WriteLine("Elements Required for Some Service Operations");
                Console.WriteLine("--------------------");

                foreach (CustomerInfo cInfo in cInfos)
                {
                    if (cInfo != null && cInfo.Id != null)
                    {
                        Console.WriteLine("CustomerId: {0}", cInfo.Id);
                        IEnumerable<AccountInfo> aInfos = GetAccounts((long)cInfo.Id);

                        // Print a list of all accounts managed by the customer
                        foreach (AccountInfo aInfo in aInfos)
                        {
                            if (aInfo != null)
                            {
                                Console.WriteLine("CustomerAccountId: {0}", aInfo.Id);
                            }
                        }
                    }
                    Console.WriteLine();
                }

                Console.WriteLine("--------------------");
                Console.WriteLine("Elements Not Required and Reserved for Future Use");
                Console.WriteLine("--------------------");
                Console.WriteLine("ApplicationToken");
                Console.WriteLine();

                _service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (e.InnerException != null)
                {
                    Console.WriteLine("\n" + e.InnerException.Message);
                }

                if (_service != null)
                {
                    _service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (_service != null)
                {
                    _service.Abort();
                }
            }
            catch (Exception e)
            {
                // Ignore fault exceptions that we already caught.

                if (!(e.InnerException is FaultException))
                {
                    Console.WriteLine(e.Message);
                }

                if (_service != null)
                {
                    _service.Abort();
                }
            }
        }

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

        static IEnumerable<CustomerInfo> GetCustomers(string filter, int topN)
        {
            try
            {
                var request = new GetCustomersInfoRequest
                    {
                        UserName = UserName,
                        Password = Password,
                        DeveloperToken = DeveloperToken,
                        ApplicationScope = ApplicationType.Advertiser,
                        CustomerNameFilter = filter,
                        TopN = topN
                    };

                return _service.GetCustomersInfo(request).CustomersInfo;
            }
            // Customer Management service operations can throw AdApiFaultDetail.
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("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.
                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("AdApiError");
                    Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        case 105:   // InvalidCredentials
                            break;
                        default:
                            Console.WriteLine("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 (FaultException<ApiFault> fault)
            {
                // Log this fault.

                Console.WriteLine("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.
                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    Console.WriteLine("OperationError");
                    Console.WriteLine("Code: {0}\nMessage: {1}\n", error.Code, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        case 106:   // UserIsNotAuthorized
                            break;
                        default:
                            Console.WriteLine("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 IEnumerable<AccountInfo> GetAccounts(long customerId)
        {
            try
            {
                var request = new GetAccountsInfoRequest
                    {
                        UserName = UserName,
                        Password = Password,
                        DeveloperToken = DeveloperToken,
                        CustomerId = customerId,
                        OnlyParentAccounts = true
                    };

                // Request only parent accounts such that the returned accounts
                // are within the current customer identifier, and not managed
                // through an agency or reseller. 

                return _service.GetAccountsInfo(request).AccountsInfo;
            }
            // Customer Management service operations can throw AdApiFaultDetail.
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("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.
                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("AdApiError");
                    Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        case 105:   // InvalidCredentials
                            break;
                        default:
                            Console.WriteLine("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 (FaultException<ApiFault> fault)
            {
                // Log this fault.

                Console.WriteLine("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.
                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    Console.WriteLine("OperationError");
                    Console.WriteLine("Code: {0}\nMessage: {1}\n", error.Code, error.Message);

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

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

Community Additions

Show:
© 2014 Microsoft