コード例 #1
0
    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="adGroupId">ID of the ad group that contains the ad.</param>
    /// <param name="adId">ID of the ad to be upgraded.</param>
    public void Run(AdWordsUser user, long adGroupId, long adId) {
      // Get the AdGroupAdService.
      AdGroupAdService adGroupAdService = (AdGroupAdService)
          user.GetService(AdWordsService.v201506.AdGroupAdService);

      try {
        // Retrieve the Ad.
        AdGroupAd adGroupAd = GetAdGroupAd(adGroupAdService, adGroupId, adId);

        if (adGroupAd == null) {
          Console.WriteLine("Ad not found.");
          return;
        }

        // Copy the destination url to the final url.
        AdUrlUpgrade upgradeUrl = new AdUrlUpgrade();
        upgradeUrl.adId = adGroupAd.ad.id;
        upgradeUrl.finalUrl = adGroupAd.ad.url;

        // Upgrade the ad.
        Ad[] upgradedAds = adGroupAdService.upgradeUrl(new AdUrlUpgrade[] { upgradeUrl });

        // Display the results.
        if (upgradedAds != null && upgradedAds.Length > 0) {
          foreach (Ad upgradedAd in upgradedAds) {
            Console.WriteLine("Ad with id = {0} and destination url = {1} was upgraded.",
                upgradedAd.id, upgradedAd.finalUrls[0]);
          }
        } else {
          Console.WriteLine("No ads were upgraded.");
        }
      } catch (Exception e) {
        throw new System.ApplicationException("Failed to upgrade ads.", e);
      }
    }