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.v201705

Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <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)
      ' Get the MediaService.
      Dim mediaService As MediaService = CType(user.GetService( _
          AdWordsService.v201705.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.
      Dim predicate As New Predicate
      predicate.operator = PredicateOperator.IN
      predicate.field = "Type"
      predicate.values = New String() {MediaMediaType.VIDEO.ToString(), _
          MediaMediaType.IMAGE.ToString()}

      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])
                Dim dimensions As Dictionary(Of MediaSize, Dimensions) = _
                    CreateMediaDimensionMap(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 Sub

    ''' <summary>
    ''' Converts an array of Media_Size_DimensionsMapEntry into a dictionary.
    ''' </summary>
    ''' <param name="dimensions">The array of Media_Size_DimensionsMapEntry to be
    ''' converted into a dictionary.</param>
    ''' <returns>A dictionary with key as MediaSize, and value as Dimensions.
    ''' </returns>
    Private Function CreateMediaDimensionMap(ByVal dimensions As Media_Size_DimensionsMapEntry()) _
            As Dictionary(Of MediaSize, Dimensions)
      Dim mediaMap As New Dictionary(Of MediaSize, Dimensions)
      For Each dimension As Media_Size_DimensionsMapEntry In dimensions
        mediaMap.Add(dimension.key, dimension.value)
      Next
      Return mediaMap
    End Function
  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.v201705
Imports Google.Api.Ads.Common.Util

Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <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)
      ' Get the MediaService.
      Dim mediaService As MediaService = CType(user.GetService( _
          AdWordsService.v201705.MediaService), MediaService)

      ' Create the image.
      Dim image As New Image
      image.data = MediaUtilities.GetAssetDataFromUrl("http://goo.gl/HJM3L")
      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)
          Dim dimensions As Dictionary(Of MediaSize, Dimensions) = _
                CreateMediaDimensionMap(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 Sub

    ''' <summary>
    ''' Converts an array of Media_Size_DimensionsMapEntry into a dictionary.
    ''' </summary>
    ''' <param name="dimensions">The array of Media_Size_DimensionsMapEntry to
    ''' be converted into a dictionary.</param>
    ''' <returns>A dictionary with key as MediaSize, and value as Dimensions.
    ''' </returns>
    Private Function CreateMediaDimensionMap(ByVal dimensions As Media_Size_DimensionsMapEntry()) _
        As Dictionary(Of MediaSize, Dimensions)
      Dim mediaMap As New Dictionary(Of MediaSize, Dimensions)
      For Each dimension As Media_Size_DimensionsMapEntry In dimensions
        mediaMap.Add(dimension.key, dimension.value)
      Next
      Return mediaMap
    End Function
  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.v201705
Imports Google.Api.Ads.Common.Util

Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <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)
      ' Get the MediaService.
      Dim mediaService As MediaService = CType(user.GetService( _
          AdWordsService.v201705.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)
          Dim dimensions As Dictionary(Of MediaSize, Dimensions) = _
              CreateMediaDimensionMap(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 Sub

    ''' <summary>
    ''' Converts an array of Media_Size_DimensionsMapEntry into a dictionary.
    ''' </summary>
    ''' <param name="dimensions">The array of Media_Size_DimensionsMapEntry to
    ''' be converted into a dictionary.</param>
    ''' <returns>A dictionary with key as MediaSize, and value as Dimensions.
    ''' </returns>
    Private Function CreateMediaDimensionMap(ByVal dimensions As Media_Size_DimensionsMapEntry()) _
        As Dictionary(Of MediaSize, Dimensions)
      Dim mediaMap As New Dictionary(Of MediaSize, Dimensions)
      For Each dimension As Media_Size_DimensionsMapEntry In dimensions
        mediaMap.Add(dimension.key, dimension.value)
      Next
      Return mediaMap
    End Function
  End Class
End Namespace

Send feedback about...

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