Miscellaneous Samples

The code samples below provide examples of miscellaneous management functions available in the AdWords API. Client Library.

Get all videos and images

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.v201710

Namespace Google.Api.Ads.AdWords.Examples.VB.v201710

  ''' <summary>
  ''' This code example gets all videos and images. To upload video, see
  ''' http://adwords.google.com/support/aw/bin/answer.py?hl=en&amp;answer=39454.
  ''' To upload image, run UploadImage.vb.
  ''' </summary>
  Public Class GetAllVideosAndImages
    Inherits ExampleBase

    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New GetAllVideosAndImages
      Console.WriteLine(codeExample.Description)
      Try
        codeExample.Run(New AdWordsUser)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}",
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example gets all videos and images. To upload video, see " &
            "http://adwords.google.com/support/aw/bin/answer.py?hl=en&amp;answer=39454. To " &
            "upload image, run UploadImage.vb."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    Public Sub Run(ByVal user As AdWordsUser)
      Using mediaService As MediaService = CType(user.GetService(
          AdWordsService.v201710.MediaService), MediaService)

        ' Create the video selector.
        Dim selector As New Selector
        selector.fields = New String() {
          Media.Fields.MediaId, Dimensions.Fields.Width,
          Dimensions.Fields.Height, Media.Fields.MimeType
        }

        ' Set the filter.
        selector.predicates = New Predicate() {
          Predicate.In(Media.Fields.Type, New String() {
            MediaMediaType.VIDEO.ToString(),
            MediaMediaType.IMAGE.ToString()
          })
        }

        selector.paging = Paging.Default

        Dim page As New MediaPage

        Try
          Do
            page = mediaService.get(selector)

            If ((Not page Is Nothing) AndAlso (Not page.entries Is Nothing)) Then
              Dim i As Integer = selector.paging.startIndex

              For Each media As Media In page.entries
                If TypeOf media Is Video Then
                  Dim video As Video = CType(media, [Video])
                  Console.WriteLine("{0}) Video with id '{1}' and name '{2}' was found.",
                      i + 1, video.mediaId, video.name)
                ElseIf TypeOf media Is Image Then
                  Dim image As Image = CType(media, [Image])

                  ' Preferred: Use image.dimensions.ToDict() if you are not on Mono.
                  Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                      MapEntryExtensions.ToDict(Of MediaSize, Dimensions)(image.dimensions)

                  Console.WriteLine("{0}) Image with id '{1}', dimensions '{2}x{3}', and MIME " &
                      "type '{4}' was found.", i + 1, image.mediaId,
                      dimensions(MediaSize.FULL).width,
                      dimensions(MediaSize.FULL).height, image.mimeType)
                End If
                i = i + 1
              Next
            End If
            selector.paging.IncreaseOffset()
          Loop While (selector.paging.startIndex < page.totalNumEntries)
          Console.WriteLine("Number of images and videos found: {0}", page.totalNumEntries)
        Catch e As Exception
          Throw New System.ApplicationException("Failed to get images and videos.", e)
        End Try
      End Using
    End Sub

  End Class

End Namespace

Upload an image

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.v201710
Imports Google.Api.Ads.Common.Util

Namespace Google.Api.Ads.AdWords.Examples.VB.v201710

  ''' <summary>
  ''' This code example uploads an image. To get images, run GetAllVideosAndImages.vb.
  ''' </summary>
  Public Class UploadImage
    Inherits ExampleBase

    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New UploadImage
      Console.WriteLine(codeExample.Description)
      Try
        codeExample.Run(New AdWordsUser)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}",
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example uploads an image. To get images, run GetAllVideosAndImages.vb."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    Public Sub Run(ByVal user As AdWordsUser)
      Using mediaService As MediaService = CType(user.GetService(
          AdWordsService.v201710.MediaService), MediaService)

        ' Create the image.
        Dim image As New Image
        image.data = MediaUtilities.GetAssetDataFromUrl("https://goo.gl/3b9Wfh")
        image.type = MediaMediaType.IMAGE

        Try
          ' Upload the image.
          Dim result As Media() = mediaService.upload(New Media() {image})

          ' Display the results.
          If ((Not result Is Nothing) AndAlso (result.Length > 0)) Then
            Dim newImage As Media = result(0)

            ' Preferred: Use newImage.dimensions.ToDict() if you are not on Mono.
            Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                MapEntryExtensions.ToDict(Of MediaSize, Dimensions)(newImage.dimensions)

            Console.WriteLine("Image with id '{0}', dimensions '{1}x{2}', and MIME type '{3}'" &
                " was uploaded.", newImage.mediaId, dimensions.Item(MediaSize.FULL).width,
                dimensions.Item(MediaSize.FULL).height, newImage.mimeType)
          Else
            Console.WriteLine("No images were uploaded.")
          End If
        Catch e As Exception
          Throw New System.ApplicationException("Failed to upload images.", e)
        End Try
      End Using
    End Sub

  End Class

End Namespace

Upload an HTML5 zip file as a MediaBundle

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.v201710
Imports Google.Api.Ads.Common.Util

Namespace Google.Api.Ads.AdWords.Examples.VB.v201710

  ''' <summary>
  ''' This code example uploads an HTML5 zip file.
  ''' </summary>
  Public Class UploadMediaBundle
    Inherits ExampleBase

    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New UploadMediaBundle
      Console.WriteLine(codeExample.Description)
      Try
        codeExample.Run(New AdWordsUser)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}",
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example uploads an HTML5 zip file."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    Public Sub Run(ByVal user As AdWordsUser)
      Using mediaService As MediaService = CType(user.GetService(
          AdWordsService.v201710.MediaService), MediaService)

        Try
          ' Create HTML5 media.
          Dim html5Zip As Byte() = MediaUtilities.GetAssetDataFromUrl("https://goo.gl/9Y7qI2")
          ' Create a media bundle containing the zip file with all the HTML5 components.
          Dim mediaBundleArray(1) As Media

          Dim mediaBundle As New MediaBundle()
          mediaBundle.data = html5Zip
          mediaBundle.type = MediaMediaType.MEDIA_BUNDLE

          mediaBundleArray(0) = mediaBundle

          ' Upload HTML5 zip.
          mediaBundleArray = mediaService.upload(mediaBundleArray)

          ' Display HTML5 zip.
          If (Not mediaBundleArray Is Nothing) AndAlso (mediaBundleArray.Length > 0) Then
            Dim newBundle As Media = mediaBundleArray(0)

            ' Preferred: Use newBundle.dimensions.ToDict() if you are not on Mono.
            Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                      MapEntryExtensions.ToDict(Of MediaSize, Dimensions)(newBundle.dimensions)

            Console.WriteLine("HTML5 media with id '{0}', dimensions '{1}x{2}', and " &
                "MIME type '{3}' was uploaded.", newBundle.mediaId,
                dimensions(MediaSize.FULL).width, dimensions(MediaSize.FULL).height,
                newBundle.mimeType)
          Else
            Console.WriteLine("No HTML5 zip was uploaded.")
          End If
        Catch e As Exception
          Throw New System.ApplicationException("Failed to upload HTML5 zip file.", e)
        End Try
      End Using
    End Sub

  End Class

End Namespace

Send feedback about...

AdWords API
AdWords API
Need help? Visit our support page.