Miscellaneous Samples

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

Get all image assets

// Copyright 2018 Google LLC
//
// 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.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;

using System;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
    /// <summary>
    /// This code example gets all image assets. To upload an image asset, run UploadImageAsset.cs.
    /// </summary>
    public class GetAllImageAssets : ExampleBase
    {
        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            GetAllImageAssets codeExample = new GetAllImageAssets();
            Console.WriteLine(codeExample.Description);
            try
            {
                codeExample.Run(new AdWordsUser());
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            get
            {
                return "This code example gets all image assets. To upload an image asset, run " +
                    "UploadImageAsset.cs.";
            }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (AssetService assetService =
                (AssetService) user.GetService(AdWordsService.v201809.AssetService))
            {
                // Create the selector.
                Selector selector = new Selector()
                {
                    fields = new string[]
                    {
                        Asset.Fields.AssetName,
                        Asset.Fields.AssetStatus,
                        ImageAsset.Fields.ImageFileSize,
                        ImageDimensionInfo.Fields.ImageWidth,
                        ImageDimensionInfo.Fields.ImageHeight,
                        ImageDimensionInfo.Fields.ImageFullSizeUrl
                    },
                    predicates = new Predicate[]
                    {
                        // Filter for image assets only.
                        Predicate.Equals(Asset.Fields.AssetSubtype, AssetType.IMAGE.ToString())
                    },
                    paging = Paging.Default
                };

                AssetPage page = new AssetPage();

                try
                {
                    do
                    {
                        // Get the image assets.
                        page = assetService.get(selector);

                        // Display the results.
                        if (page != null && page.entries != null)
                        {
                            int i = selector.paging.startIndex;
                            foreach (ImageAsset imageAsset in page.entries)
                            {
                                Console.WriteLine(
                                    "{0}) Image asset with id = '{1}', name = '{2}' and " +
                                    "status = '{3}' was found.", i + 1, imageAsset.assetId,
                                    imageAsset.assetName, imageAsset.assetStatus);
                                Console.WriteLine("  Size is {0}x{1} and asset URL is {2}.",
                                    imageAsset.fullSizeInfo.imageWidth,
                                    imageAsset.fullSizeInfo.imageHeight,
                                    imageAsset.fullSizeInfo.imageUrl);
                                i++;
                            }
                        }

                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);

                    Console.WriteLine("Number of image assets found: {0}", page.totalNumEntries);
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to retrieve image assets.", e);
                }
            }
        }
    }
}

Get all images and videos

// Copyright 2018 Google LLC
//
// 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.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;

using System;
using System.Collections.Generic;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
    /// <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.cs.
    /// </summary>
    public class GetAllVideosAndImages : ExampleBase
    {
        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            GetAllVideosAndImages codeExample = new GetAllVideosAndImages();
            Console.WriteLine(codeExample.Description);
            try
            {
                codeExample.Run(new AdWordsUser());
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            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.cs.";
            }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (MediaService mediaService =
                (MediaService) user.GetService(AdWordsService.v201809.MediaService))
            {
                // Create a selector.
                Selector selector = new Selector()
                {
                    fields = new string[]
                    {
                        Media.Fields.MediaId,
                        Dimensions.Fields.Width,
                        Dimensions.Fields.Height,
                        Media.Fields.MimeType
                    },
                    predicates = new Predicate[]
                    {
                        Predicate.In(Media.Fields.Type, new string[]
                        {
                            MediaMediaType.VIDEO.ToString(),
                            MediaMediaType.IMAGE.ToString()
                        })
                    },
                    paging = Paging.Default
                };
                MediaPage page = new MediaPage();

                try
                {
                    do
                    {
                        page = mediaService.get(selector);

                        if (page != null && page.entries != null)
                        {
                            int i = selector.paging.startIndex;

                            foreach (Media media in page.entries)
                            {
                                if (media is Video)
                                {
                                    Video video = (Video) media;
                                    Console.WriteLine(
                                        "{0}) Video with id '{1}' and name '{2}' was found.", i + 1,
                                        video.mediaId, video.name);
                                }
                                else if (media is Image)
                                {
                                    Image image = (Image) media;
                                    Dictionary<MediaSize, Dimensions> dimensions =
                                        image.dimensions.ToDict();
                                    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);
                                }

                                i++;
                            }
                        }

                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);

                    Console.WriteLine("Number of images and videos found: {0}",
                        page.totalNumEntries);
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to get images and videos.", e);
                }
            }
        }
    }
}

Upload an image

// Copyright 2018 Google LLC
//
// 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.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;
using Google.Api.Ads.Common.Util;

using System;
using System.Collections.Generic;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
    /// <summary>
    /// This code example uploads an image. To get images, run GetAllVideosAndImages.cs.
    /// </summary>
    public class UploadImage : ExampleBase
    {
        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            UploadImage codeExample = new UploadImage();
            Console.WriteLine(codeExample.Description);
            try
            {
                codeExample.Run(new AdWordsUser());
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            get
            {
                return "This code example uploads an image. To get images, " +
                    "run GetAllVideosAndImages.cs.";
            }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (MediaService mediaService =
                (MediaService) user.GetService(AdWordsService.v201809.MediaService))
            {
                // Create the image.
                Image image = new Image
                {
                    data = MediaUtilities.GetAssetDataFromUrl("https://goo.gl/3b9Wfh", user.Config),
                    type = MediaMediaType.IMAGE
                };

                try
                {
                    // Upload the image.
                    Media[] result = mediaService.upload(new Media[]
                    {
                        image
                    });

                    // Display the results.
                    if (result != null && result.Length > 0)
                    {
                        Media newImage = result[0];
                        Dictionary<MediaSize, Dimensions> dimensions = newImage.dimensions.ToDict();
                        Console.WriteLine(
                            "Image with id '{0}', dimensions '{1}x{2}', and MIME type '{3}'" +
                            " was uploaded.", newImage.mediaId, dimensions[MediaSize.FULL].width,
                            dimensions[MediaSize.FULL].height, newImage.mimeType);
                    }
                    else
                    {
                        Console.WriteLine("No images were uploaded.");
                    }
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to upload image.", e);
                }
            }
        }
    }
}

Upload an image asset

// Copyright 2018 Google LLC
//
// 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.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;
using Google.Api.Ads.Common.Util;

using System;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
    /// <summary>
    /// This code example uploads an image asset. To get images, run GetAllImageAssets.cs.
    /// </summary>
    public class UploadImageAsset : ExampleBase
    {
        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            UploadImageAsset codeExample = new UploadImageAsset();
            Console.WriteLine(codeExample.Description);
            try
            {
                codeExample.Run(new AdWordsUser());
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            get
            {
                return "This code example uploads an image asset. To get images, run " +
                    "GetAllImageAssets.cs.";
            }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (AssetService assetService =
                (AssetService) user.GetService(AdWordsService.v201809.AssetService))
            {
                // Create the image asset.
                ImageAsset imageAsset = new ImageAsset()
                {
                    // Optional: Provide a unique friendly name to identify your asset. If you
                    // specify the assetName field, then both the asset name and the image being
                    // uploaded should be unique, and should not match another ACTIVE asset in this
                    // customer account.
                    // assetName = "Jupiter Trip " + ExampleUtilities.GetRandomString(),
                    imageData =
                        MediaUtilities.GetAssetDataFromUrl("https://goo.gl/3b9Wfh", user.Config),
                };

                // Create the operation.
                AssetOperation operation = new AssetOperation()
                {
                    @operator = Operator.ADD,
                    operand = imageAsset
                };

                try
                {
                    // Create the asset.
                    AssetReturnValue result = assetService.mutate(new AssetOperation[]
                    {
                        operation
                    });

                    // Display the results.
                    if (result != null && result.value != null && result.value.Length > 0)
                    {
                        Asset newAsset = result.value[0];

                        Console.WriteLine("Image asset with id = '{0}' and name = {1} was created.",
                            newAsset.assetId, newAsset.assetName);
                    }
                    else
                    {
                        Console.WriteLine("No image asset was created.");
                    }
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to create image asset.", e);
                }
            }
        }
    }
}

Upload an HTML5 zip file as a MediaBundle

// Copyright 2018 Google LLC
//
// 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.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;
using Google.Api.Ads.Common.Util;

using System;
using System.Collections.Generic;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
    /// <summary>
    /// This code example uploads an HTML5 zip file.
    /// </summary>
    public class UploadMediaBundle : ExampleBase
    {
        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            UploadMediaBundle codeExample = new UploadMediaBundle();
            Console.WriteLine(codeExample.Description);
            try
            {
                codeExample.Run(new AdWordsUser());
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            get { return "This example uploads an HTML5 zip file."; }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (MediaService mediaService =
                (MediaService) user.GetService(AdWordsService.v201809.MediaService))
            {
                try
                {
                    // Create HTML5 media.
                    byte[] html5Zip =
                        MediaUtilities.GetAssetDataFromUrl("https://goo.gl/9Y7qI2", user.Config);
                    // Create a media bundle containing the zip file with all the HTML5 components.
                    Media[] mediaBundle = new Media[]
                    {
                        new MediaBundle()
                        {
                            data = html5Zip,
                            type = MediaMediaType.MEDIA_BUNDLE
                        }
                    };

                    // Upload HTML5 zip.
                    mediaBundle = mediaService.upload(mediaBundle);

                    // Display HTML5 zip.
                    if (mediaBundle != null && mediaBundle.Length > 0)
                    {
                        Media newBundle = mediaBundle[0];
                        Dictionary<MediaSize, Dimensions>
                            dimensions = newBundle.dimensions.ToDict();
                        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.");
                    }
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to upload HTML5 zip file.", e);
                }
            }
        }
    }
}

フィードバックを送信...

ご不明な点がありましたら、Google のサポートページをご覧ください。