Пример #1
0
    /// <summary>
    /// Create a test company for running further tests.
    /// </summary>
    /// <returns>A creative for running further tests.</returns>
    public Creative CreateCreative(DfpUser user, long advertiserId) {
      CreativeService creativeService = (CreativeService)user.GetService(
          DfpService.v201306.CreativeService);

      // Create creative size.
      Size size = new Size();
      size.width = 300;
      size.height = 250;

      // Create an image creative.
      ImageCreative imageCreative = new ImageCreative();
      imageCreative.name = string.Format("Image creative #{0}", GetTimeStamp());
      imageCreative.advertiserId = advertiserId;
      imageCreative.destinationUrl = "http://www.google.com";
      imageCreative.size = size;

      // Create image asset.
      CreativeAsset creativeAsset = new CreativeAsset();
      creativeAsset.fileName = "image.jpg";
      creativeAsset.assetByteArray = MediaUtilities.GetAssetDataFromUrl(
          "http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg");
      creativeAsset.size = size;
      imageCreative.primaryImageAsset = creativeAsset;

      return creativeService.createCreative(imageCreative);
    }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The DFP user object running the code example.</param>
        public override void Run(DfpUser user)
        {
            // Get the CreativeService.
              CreativeService creativeService =
              (CreativeService) user.GetService(DfpService.v201306.CreativeService);

              // Set the ID of the advertiser (company) that all creatives will be
              // assigned to.
              long advertiserId = long.Parse(_T("INSERT_ADVERTISER_COMPANY_ID_HERE"));

              // Create an array to store local image creative objects.
              Creative[] imageCreatives = new ImageCreative[5];

              for (int i = 0; i < 5; i++) {
            // Create creative size.
            Size size = new Size();
            size.width = 300;
            size.height = 250;

            // Create an image creative.
            ImageCreative imageCreative = new ImageCreative();
            imageCreative.name = string.Format("Image creative #{0}", i);
            imageCreative.advertiserId = advertiserId;
            imageCreative.destinationUrl = "http://www.google.com";
            imageCreative.size = size;

            // Create image asset.
            CreativeAsset creativeAsset = new CreativeAsset();
            creativeAsset.fileName = "image.jpg";
            creativeAsset.assetByteArray = MediaUtilities.GetAssetDataFromUrl(
            "http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg");
            creativeAsset.size = size;
            imageCreative.primaryImageAsset = creativeAsset;

            imageCreatives[i] = imageCreative;
              }

              try {
            // Create the image creatives on the server.
            imageCreatives = creativeService.createCreatives(imageCreatives);

            if (imageCreatives != null) {
              foreach (Creative creative in imageCreatives) {
            // Use "is" operator to determine what type of creative was
            // returned.
            if (creative is ImageCreative) {
              ImageCreative imageCreative = (ImageCreative) creative;
              Console.WriteLine("An image creative with ID ='{0}', name ='{1}' and size = " +
                  "({2},{3}) was created and can be previewed at: {4}", imageCreative.id,
                  imageCreative.name, imageCreative.size.width, imageCreative.size.height,
                  imageCreative.previewUrl);
            } else {
              Console.WriteLine("A creative with ID ='{0}', name='{1}' and type='{2}' was created.",
                  creative.id, creative.name, creative.CreativeType);
            }
              }
            } else {
              Console.WriteLine("No creatives created.");
            }
              } catch (Exception ex) {
            Console.WriteLine("Failed to create creatives. Exception says \"{0}\"", ex.Message);
              }
        }
    public void TestCreateCreative() {
      // Create creative size.
      Size size = new Size();
      size.width = 300;
      size.height = 250;

      // Create an image creative.
      ImageCreative imageCreative = new ImageCreative();
      imageCreative.name = string.Format("Image creative #{0}", new TestUtils().GetTimeStamp());
      imageCreative.advertiserId = advertiserId;
      imageCreative.destinationUrl = "http://www.google.com";
      imageCreative.size = size;

      // Create image asset.
      CreativeAsset creativeAsset = new CreativeAsset();
      creativeAsset.fileName = "image.jpg";
      creativeAsset.assetByteArray = MediaUtilities.GetAssetDataFromUrl(
          "http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg");
      creativeAsset.size = size;
      imageCreative.primaryImageAsset = creativeAsset;

      Creative newCreative = null;

      Assert.DoesNotThrow(delegate() {
        newCreative = creativeService.createCreative(imageCreative);
      });

      Assert.NotNull(newCreative);
      Assert.That(newCreative is ImageCreative);
      Assert.AreEqual(newCreative.advertiserId, imageCreative.advertiserId);
      Assert.AreEqual(newCreative.name, imageCreative.name);
    }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The DFP user object running the code example.</param>
        public override void Run(DfpUser user)
        {
            // Get the CreativeService.
              CreativeService creativeService =
              (CreativeService) user.GetService(DfpService.v201306.CreativeService);

              long[] creativeIds = new long[] {long.Parse(_T("INSERT_IMAGE_CREATIVE_ID_HERE"))};

              // Build a comma separated list of creativeIds. Note that if you are using
              // .NET 4.0 or above, you could use the newly introduced
              // String.Join<T>(string separator, IEnumerable<T> values) method to
              // perform this task.
              string[] creativeIdTexts = new string[creativeIds.Length];
              for (int i = 0; i < creativeIdTexts.Length; i++) {
            creativeIdTexts[i] = creativeIds[i].ToString();
              }

              string commaSeparatedCreativeIds = string.Join(",", creativeIdTexts);

              // Create the statement to filter image creatives by id.
              Statement statement = new StatementBuilder(
              string.Format("WHERE id IN ({0}) and creativeType = :creativeType LIMIT 500",
              commaSeparatedCreativeIds)).AddValue("creativeType", "ImageCreative").
              ToStatement();

              try {
            // Retrieve all creatives which match.
            CreativePage page = creativeService.getCreativesByStatement(statement);

            if (page.results != null) {
              Creative[] creatives = page.results;
              long[] oldIds = new long[creatives.Length];
              for (int i = 0; i < creatives.Length; i++) {
            ImageCreative imageCreative = (ImageCreative) creatives[i];
            oldIds[i] = imageCreative.id;
            // Since we cannot set id to null, we mark it as not specified.
            imageCreative.idSpecified = false;

            imageCreative.advertiserId = imageCreative.advertiserId;
            imageCreative.name = imageCreative.name + " (Copy #" + GetTimeStamp() + ")";

            // Create image asset.
            CreativeAsset creativeAsset = new CreativeAsset();
            creativeAsset.fileName = "image.jpg";
            creativeAsset.assetByteArray = MediaUtilities.GetAssetDataFromUrl(
                imageCreative.primaryImageAsset.assetUrl);

            creativeAsset.size = imageCreative.primaryImageAsset.size;
            imageCreative.primaryImageAsset = creativeAsset;

            creatives[i] = imageCreative;
              }

              // Create the copied creative.
              creatives = creativeService.createCreatives(creatives);

              // Display copied creatives.
              for (int i = 0; i < creatives.Length; i++) {
            Console.WriteLine("Image creative with ID \"{0}\" copied to ID \"{1}\".", oldIds[i],
                creatives[i].id);
              }
            } else {
              Console.WriteLine("No creatives were copied.");
            }
              } catch (Exception ex) {
            Console.WriteLine("Failed to copy creatives. Exception says \"{0}\"", ex.Message);
              }
        }
    public void TestCreateCreatives() {
      // Create an array to store local image creative objects.
      Creative[] imageCreatives = new ImageCreative[2];

      for (int i = 0; i < 2; i++) {
        // Create creative size.
        Size size = new Size();
        size.width = 300;
        size.height = 250;

        // Create an image creative.
        ImageCreative imageCreative = new ImageCreative();
        imageCreative.name = string.Format("Image creative #{0}", i);
        imageCreative.advertiserId = advertiserId;
        imageCreative.destinationUrl = "http://www.google.com";
        imageCreative.size = size;

        // Create image asset.
        CreativeAsset creativeAsset = new CreativeAsset();
        creativeAsset.fileName = "image.jpg";
        creativeAsset.assetByteArray = MediaUtilities.GetAssetDataFromUrl(
            "http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg");
        creativeAsset.size = size;
        imageCreative.primaryImageAsset = creativeAsset;

        imageCreatives[i] = imageCreative;
      }

      Creative[] newCreatives = null;

      Assert.DoesNotThrow(delegate() {
        newCreatives = creativeService.createCreatives(imageCreatives);
      });

      Assert.NotNull(newCreatives);
      Assert.AreEqual(newCreatives.Length, 2);
      Assert.NotNull(newCreatives[0]);
      Assert.That(newCreatives[0] is ImageCreative);
      Assert.AreEqual(newCreatives[0].advertiserId, advertiserId);
      Assert.AreEqual(newCreatives[0].name, "Image creative #0");
      Assert.NotNull(newCreatives[1]);
      Assert.That(newCreatives[1] is ImageCreative);
      Assert.AreEqual(newCreatives[1].advertiserId, advertiserId);
      Assert.AreEqual(newCreatives[1].name, "Image creative #1");
    }