Export (0) Print
Expand All

Get SOAP Header Elements in Visual Basic

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.


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.ServiceModel

' BingAds.CustomerManagement is the application-defined namespace of 
' the service reference used for this example. 

Imports GetHeaders.BingAds.CustomerManagement


Module GetHeaders

    'Specify your credentials.

    Public Const UserName = "<UserNameGoesHere>"
    Public Const Password = "<PasswordGoesHere>"
    Public Const DeveloperToken = "<DeveloperTokenGoesHere>"

    Public _service As CustomerManagementServiceClient

    Sub Main()
        Try
            _service = New CustomerManagementServiceClient

            ' Given the user credentials provided above, retrieve the customers permitted
            Dim cInfos As IEnumerable(Of CustomerInfo) = GetCustomers(String.Empty, 100, ApplicationType.Advertiser)

            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("--------------------")

            If cInfos IsNot Nothing Then
                For Each cInfo As CustomerInfo In cInfos
                    Console.WriteLine("CustomerId: {0}", cInfo.Id)

                    ' Request only parent accounts such that the returned accounts
                    ' are within the current customer identifier, and not managed
                    ' through an agency or reseller. 
                    Dim aInfos As IEnumerable(Of AccountInfo) = GetAccounts(cInfo.Id, True)

                    If aInfos IsNot Nothing Then
                        For Each aInfo As AccountInfo In aInfos
                            Console.WriteLine("CustomerAccountId: {0}", aInfo.Id)
                        Next
                    End If
                    Console.WriteLine()
                Next
            End If


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

        Catch ex As CommunicationException
            Console.WriteLine(ex.Message)

            If (_service IsNot Nothing) Then
                _service.Abort()
            End If

        Catch ex As TimeoutException
            Console.WriteLine(ex.Message)

            If (_service IsNot Nothing) Then
                _service.Abort()
            End If

        Catch ex As Exception
            Console.WriteLine(ex.Message)

            If (_service IsNot Nothing) Then
                _service.Abort()
            End If

        End Try

    End Sub

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

    Private Function GetCustomers( _
    ByRef cNameFilter As String, _
    ByRef topN As Integer, _
    ByRef applicationScope As ApplicationType) As IEnumerable(Of CustomerInfo)

        ' Define the variables to use in this application.
        Dim request As GetCustomersInfoRequest
        Dim response As GetCustomersInfoResponse
        Dim cInfos As IList(Of CustomerInfo) = Nothing

        Try
            ' Get the list of customers
            request = New GetCustomersInfoRequest(String.Empty, DeveloperToken, Password, UserName, cNameFilter, topN, applicationScope)
            response = _service.GetCustomersInfo(request)
            cInfos = response.CustomersInfo.ToArray

        Catch fault As FaultException(Of AdApiFaultDetail)
            Dim detail As AdApiFaultDetail = fault.Detail

            ' Display any service operation error information.
            For Each apiError As AdApiError In detail.Errors
                Console.Write("Operation error")
                Console.WriteLine( _
                    " '{0}' ({1}) encountered.", _
                    apiError.Message, _
                    apiError.Code)
            Next

        Catch fault As FaultException(Of ApiFault)
            Dim detail As ApiFault = fault.Detail

            ' Display any service operation error information.
            For Each opError As OperationError In detail.OperationErrors
                Console.Write("Operation error")
                Console.WriteLine( _
                    " '{0}' ({1}) encountered.", _
                    opError.Message, _
                    opError.Code)
            Next

        Catch e As Exception
            ' Add your additional exception-handling code here.
            ' Capture exceptions on the client that are unrelated to
            ' the Bing Ads API. An example would be an
            ' out-of-memory condition on the client.
            Console.WriteLine("Error '{0}' encountered.", e.Message)
        End Try

        GetCustomers = cInfos

    End Function


    ' Gets the accounts info that the customer owns and manages

    Private Function GetAccounts( _
    ByRef customerId As Integer, _
    ByRef onlyParentAccounts As Boolean) As IEnumerable(Of AccountInfo)

        ' Define the variables to use in this application.
        Dim request As GetAccountsInfoRequest
        Dim response As GetAccountsInfoResponse
        Dim aInfos As IList(Of AccountInfo) = Nothing

        Try
            ' Get the list of accounts
            request = New GetAccountsInfoRequest(String.Empty, DeveloperToken, Password, UserName, customerId, onlyParentAccounts)
            response = _service.GetAccountsInfo(request)
            aInfos = response.AccountsInfo.ToArray

        Catch fault As FaultException(Of AdApiFaultDetail)
            Dim detail As AdApiFaultDetail = fault.Detail

            ' Display any service operation error information.
            For Each apiError As AdApiError In detail.Errors
                Console.Write("Operation error")
                Console.WriteLine( _
                    " '{0}' ({1}) encountered.", _
                    apiError.Message, _
                    apiError.Code)
            Next

        Catch fault As FaultException(Of ApiFault)
            Dim detail As ApiFault = fault.Detail

            ' Display any service operation error information.
            For Each opError As OperationError In detail.OperationErrors
                Console.Write("Operation error")
                Console.WriteLine( _
                    " '{0}' ({1}) encountered.", _
                    opError.Message, _
                    opError.Code)
            Next

        Catch e As Exception
            ' Add your additional exception-handling code here.
            ' Capture exceptions on the client that are unrelated to
            ' the Bing Ads API. An example would be an
            ' out-of-memory condition on the client.
            Console.WriteLine("Error '{0}' encountered.", e.Message)
        End Try

        GetAccounts = aInfos

    End Function

End Module



Community Additions

Show:
© 2014 Microsoft