/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="labelName">ID of the label.</param> public void Run(AdWordsUser user, long labelId) { // Get the CampaignService. CampaignService campaignService = (CampaignService) user.GetService(AdWordsService.v201509.CampaignService); // Create the selector. Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Labels }, predicates = new Predicate[] { // Labels filtering is performed by ID. You can use CONTAINS_ANY to // select campaigns with any of the label IDs, CONTAINS_ALL to select // campaigns with all of the label IDs, or CONTAINS_NONE to select // campaigns with none of the label IDs. Predicate.ContainsAny(Campaign.Fields.Labels, new string[] { labelId.ToString() }) }, paging = Paging.Default }; CampaignPage page = new CampaignPage(); try { do { // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (Campaign campaign in page.entries) { List<string> labelNames = new List<string>(); foreach (Label label in campaign.labels) { labelNames.Add(label.name); } Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and labels = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, string.Join(", ", labelNames.ToArray())); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns by label", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the CampaignService. CampaignService campaignService = (CampaignService) user.GetService(AdWordsService.v201601.CampaignService); // Create the query. string query = "SELECT Id, Name, Status ORDER BY Name"; int offset = 0; int pageSize = 500; CampaignPage page = new CampaignPage(); try { do { string queryWithPaging = string.Format("{0} LIMIT {1}, {2}", query, offset, pageSize); // Get the campaigns. page = campaignService.query(queryWithPaging); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the CampaignService. CampaignService campaignService = (CampaignService) user.GetService(AdWordsService.v201509.CampaignService); // Create the selector. Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status }, paging = Paging.Default }; CampaignPage page = new CampaignPage(); try { do { // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns", e); } }
/// <summary> /// The main method. /// </summary> /// <param name="args">Command line arguments.</param> static void Main(string[] args) { AdWordsUser user = new AdWordsUser(); // This code example shows how to run an AdWords API web application // while incorporating the OAuth2 installed application flow into your // application. If your application uses a single MCC login to make calls // to all your accounts, you shouldn't use this code example. Instead, you // should run OAuthTokenGenerator.exe to generate a refresh // token and use that configuration in your application's App.config. AdWordsAppConfig config = user.Config as AdWordsAppConfig; if (user.Config.OAuth2Mode == OAuth2Flow.APPLICATION && string.IsNullOrEmpty(config.OAuth2RefreshToken)) { DoAuth2Authorization(user); } Console.Write("Enter the customer id: "); string customerId = Console.ReadLine(); config.ClientCustomerId = customerId; // Get the CampaignService. CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201406.CampaignService); // Create the selector. Selector selector = new Selector(); selector.fields = new string[] { "Id", "Name", "Status" }; // Set the selector paging. selector.paging = new Paging(); int offset = 0; int pageSize = 500; CampaignPage page = new CampaignPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve campaigns", ex); } }
/// <summary> /// Updates the campaign DSA setting to add DSA pagefeeds. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">The Campaign ID.</param> /// <param name="feedId">The page feed ID.</param> private static void UpdateCampaignDsaSetting(AdWordsUser user, long campaignId, long feedId) { using (CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201802.CampaignService)) { Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Settings }, predicates = new Predicate[] { Predicate.Equals(Campaign.Fields.Id, campaignId) }, paging = Paging.Default }; CampaignPage page = campaignService.get(selector); if (page == null || page.entries == null || page.entries.Length == 0) { throw new System.ApplicationException( string.Format("Failed to retrieve campaign with ID = {0}.", campaignId)); } Campaign campaign = page.entries[0]; if (campaign.settings == null) { throw new System.ApplicationException("This is not a DSA campaign."); } DynamicSearchAdsSetting dsaSetting = null; Setting[] campaignSettings = campaign.settings; for (int i = 0; i < campaign.settings.Length; i++) { Setting setting = campaignSettings[i]; if (setting is DynamicSearchAdsSetting) { dsaSetting = (DynamicSearchAdsSetting)setting; break; } } if (dsaSetting == null) { throw new System.ApplicationException("This is not a DSA campaign."); } // Use a page feed to specify precisely which URLs to use with your // Dynamic Search Ads. dsaSetting.pageFeed = new PageFeed() { feedIds = new long[] { feedId }, }; // Optional: Specify whether only the supplied URLs should be used with your // Dynamic Search Ads. dsaSetting.useSuppliedUrlsOnly = true; Campaign campaignToUpdate = new Campaign { id = campaignId, settings = campaignSettings }; CampaignOperation operation = new CampaignOperation { operand = campaignToUpdate, @operator = Operator.SET }; try { CampaignReturnValue retval = campaignService.mutate(new CampaignOperation[] { operation }); Campaign updatedCampaign = retval.value[0]; Console.WriteLine( "DSA page feed for campaign ID '{0}' was updated with feed ID '{1}'.", updatedCampaign.id, feedId); } catch (Exception e) { throw new System.ApplicationException("Failed to set page feed for campaign.", e); } } }
/// <summary> /// The main method. /// </summary> /// <param name="args">Command line arguments.</param> static void _Main(string[] args) { AdWordsUser user = new AdWordsUser(); // This code example shows how to run an AdWords API web application // while incorporating the OAuth2 installed application flow into your // application. If your application uses a single AdWords manager account // login to make calls to all your accounts, you shouldn't use this code // example. Instead, you should run OAuthTokenGenerator.exe to generate a // refresh token and use that configuration in your application's // App.config. AdWordsAppConfig config = user.Config as AdWordsAppConfig; if (user.Config.OAuth2Mode == OAuth2Flow.APPLICATION && string.IsNullOrEmpty(config.OAuth2RefreshToken)) { DoAuth2Authorization(user); } Console.Write("Enter the customer id: "); string customerId = Console.ReadLine(); config.ClientCustomerId = customerId; // Get the CampaignService. CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201802.CampaignService); Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status }, ordering = new OrderBy[] { OrderBy.Asc(Campaign.Fields.Name) }, paging = Paging.Default }; CampaignPage page = new CampaignPage(); try { do { // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="labelId">ID of the label.</param> public void Run(AdWordsUser user, long labelId) { using (CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201809.CampaignService)) { // Create the selector. Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Labels }, predicates = new Predicate[] { // Labels filtering is performed by ID. You can use CONTAINS_ANY to // select campaigns with any of the label IDs, CONTAINS_ALL to select // campaigns with all of the label IDs, or CONTAINS_NONE to select // campaigns with none of the label IDs. Predicate.ContainsAny(Campaign.Fields.Labels, new string[] { labelId.ToString() }) }, paging = Paging.Default }; CampaignPage page = new CampaignPage(); try { do { // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (Campaign campaign in page.entries) { List <string> labelNames = new List <string>(); foreach (Label label in campaign.labels) { labelNames.Add(label.name); } Console.WriteLine( "{0}) Campaign with id = '{1}', name = '{2}' and " + "labels = '{3}' was found.", i + 1, campaign.id, campaign.name, string.Join(", ", labelNames.ToArray())); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns by label", e); } } }
public void TestHasNextPage() { SelectQueryBuilder queryBuilder = null; SelectQuery query = null; queryBuilder = new SelectQueryBuilder(); query = queryBuilder .Select("CampaignId", "Status", "Clicks", "Impressions") .Where("Clicks").Equals(20) .OrderByAscending("CampaignId") .OrderByDescending("Status") .Build(); // Tests for regular pages. CampaignPage campaignPage = new CampaignPage(); // Start index is less than total number of entries. query.Limit(1, 20); campaignPage.totalNumEntries = 15; Assert.IsTrue(query.HasNextPage(campaignPage)); // Start index is greater than total number of entries. query.Limit(18, 20); campaignPage.totalNumEntries = 15; Assert.IsFalse(query.HasNextPage(campaignPage)); // Tests for AdGroupBidLandscapePage. AdGroupBidLandscapePage adGroupBidLandscapePage = new AdGroupBidLandscapePage(); adGroupBidLandscapePage.entries = new AdGroupBidLandscape[] { new AdGroupBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 12, impressions = 232 }, } }, new AdGroupBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } } }; // Start index is less than total number of landscape points (5). query.Limit(1, 20); Assert.IsTrue(query.HasNextPage(adGroupBidLandscapePage)); adGroupBidLandscapePage.entries = new AdGroupBidLandscape[] { new AdGroupBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] {} }, new AdGroupBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = null } }; // Start index is less than total number of landscape points (0). query.Limit(1, 20); Assert.IsFalse(query.HasNextPage(adGroupBidLandscapePage)); // Tests for CriterionBidLandscapePage. CriterionBidLandscapePage criterionBidLandscapePage = new CriterionBidLandscapePage() { entries = new CriterionBidLandscape[] { new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } }, new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } } } }; // Start index is less than total number of landscape points (5). query.Limit(1, 20); Assert.IsTrue(query.HasNextPage(criterionBidLandscapePage)); // Start index is less than total number of landscape points (0). criterionBidLandscapePage.entries = new CriterionBidLandscape[] { new CriterionBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] {} }, new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = null } }; query.Limit(1, 20); Assert.IsFalse(query.HasNextPage(criterionBidLandscapePage)); }
public void TestNextPage() { SelectQueryBuilder queryBuilder = null; SelectQuery query = null; queryBuilder = new SelectQueryBuilder(); query = queryBuilder .Select("CampaignId", "Status", "Clicks", "Impressions") .Where("Clicks").Equals(20) .OrderByAscending("CampaignId") .OrderByDescending("Status") .Build(); // Test for regular pages. CampaignPage campaignPage = new CampaignPage(); // Query limits should increment by numberResults (20). query.Limit(1, 20); query.NextPage(campaignPage); Assert.IsTrue(query.ToString().Contains("LIMIT 21, 20")); // Test for AdGroupBidLandscapePage. AdGroupBidLandscapePage adGroupBidLandscapePage = new AdGroupBidLandscapePage(); adGroupBidLandscapePage.entries = new AdGroupBidLandscape[] { new AdGroupBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 12, impressions = 232 }, } }, new AdGroupBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } } }; // Query limits should increment by total landscapePoints (5). query.Limit(1, 20); query.NextPage(adGroupBidLandscapePage); Assert.IsTrue(query.ToString().Contains("LIMIT 6, 20")); // Query limits should increment by total landscapePoints (0). adGroupBidLandscapePage.entries = new AdGroupBidLandscape[] { new AdGroupBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] {} }, new AdGroupBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = null } }; query.Limit(1, 20); query.NextPage(adGroupBidLandscapePage); Assert.IsTrue(query.ToString().Contains("LIMIT 1, 20")); CriterionBidLandscapePage criterionBidLandscapePage = new CriterionBidLandscapePage() { entries = new CriterionBidLandscape[] { new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } }, new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = new BidLandscapeLandscapePoint[] { new BidLandscapeLandscapePoint() { clicks = 10, impressions = 200 }, new BidLandscapeLandscapePoint() { clicks = 5, impressions = 232 }, new BidLandscapeLandscapePoint() { clicks = 66, impressions = 550 }, } } } }; // Query limits should increment by total landscapePoints (6). query.Limit(1, 20); query.NextPage(criterionBidLandscapePage); Assert.IsTrue(query.ToString().Contains("LIMIT 7, 20")); // Query limits should increment by total landscapePoints (0). criterionBidLandscapePage.entries = new CriterionBidLandscape[] { new CriterionBidLandscape() { campaignId = 123, adGroupId = 456, landscapePoints = new BidLandscapeLandscapePoint[] {} }, new CriterionBidLandscape() { campaignId = 125, adGroupId = 454, landscapePoints = null } }; query.Limit(1, 20); query.NextPage(criterionBidLandscapePage); Assert.IsTrue(query.ToString().Contains("LIMIT 1, 20")); }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="labelName">ID of the label.</param> public void Run(AdWordsUser user, long labelId) { // Get the CampaignService. CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201506.CampaignService); // Create the selector. Selector selector = new Selector(); selector.fields = new string[] { "Id", "Name", "Labels" }; // Labels filtering is performed by ID. You can use CONTAINS_ANY to // select campaigns with any of the label IDs, CONTAINS_ALL to select // campaigns with all of the label IDs, or CONTAINS_NONE to select // campaigns with none of the label IDs. Predicate predicate = new Predicate(); predicate.@operator = PredicateOperator.CONTAINS_ANY; predicate.field = "Labels"; predicate.values = new string[] { labelId.ToString() }; selector.predicates = new Predicate[] { predicate }; // Set the selector paging. selector.paging = new Paging(); int offset = 0; int pageSize = 500; CampaignPage page = new CampaignPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (Campaign campaign in page.entries) { List <string> labelNames = new List <string>(); foreach (Label label in campaign.labels) { labelNames.Add(label.name); } Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and labels = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, string.Join(", ", labelNames.ToArray())); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve campaigns by label", ex); } }
/// <summary> /// The main method. /// </summary> /// <param name="args">Command line arguments.</param> static void _Main(string[] args) { AdWordsUser user = new AdWordsUser(); AdWordsAppConfig config = (user.Config as AdWordsAppConfig); if (config.AuthorizationMethod == AdWordsAuthorizationMethod.OAuth2) { if (config.OAuth2Mode == OAuth2Flow.APPLICATION && string.IsNullOrEmpty(config.OAuth2RefreshToken)) { DoAuth2Authorization(user); } } else { throw new Exception("Authorization mode is not OAuth."); } Console.Write("Enter the customer id: "); string customerId = Console.ReadLine(); config.ClientCustomerId = customerId; // Get the CampaignService. CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201309.CampaignService); // Create the selector. Selector selector = new Selector(); selector.fields = new string[] { "Id", "Name", "Status" }; // Set the selector paging. selector.paging = new Paging(); int offset = 0; int pageSize = 500; CampaignPage page = new CampaignPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve campaigns", ex); } }
private void GetCampaigns_Click(object sender, EventArgs e) { Dictionary <string, string> headers = new Dictionary <string, string>() { { "DeveloperToken", this.DeveloperToken.Text }, { "UserAgent", String.Format("Edge File Manager (version {0})", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()) }, { "EnableGzipCompression", this.EnableGzipCompression.Text }, { "ClientCustomerId", this.ClientCustomerId.Text }, { "Email", this.Email.Text } }; User = new AdWordsUser(headers); try { //Getting AuthToken (User.Config as AdWordsAppConfig).AuthToken = AdwordsUtill.GetAuthToken(User); } catch (Exception exc) { this.rchtxt.Text = exc.Message + " #### " + exc.InnerException != null ? exc.InnerException.Message : string.Empty; } // Get the CampaignService. CampaignService campaignService = (CampaignService)User.GetService(AdWordsService.v201302.CampaignService); // Create the query. string query = "SELECT Id, Name, Status,Settings ORDER BY Name"; int offset = 0; int pageSize = 500; CampaignPage page = new CampaignPage(); try { do { string queryWithPaging = string.Format("{0} LIMIT {1}, {2}", query, offset, pageSize); // Get the campaigns. page = campaignService.query(queryWithPaging); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (Campaign campaign in page.entries) { this.rchtxt.AppendText(string.Format("/n Campaign id = '{0}', name = '{1}' ,status = '{2}'" + " was found.", campaign.id, campaign.name, campaign.status)); // i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve campaigns", ex); } }
/// <summary> /// The main method. /// </summary> /// <param name="args">Command line arguments.</param> static void Main(string[] args) { AdWordsUser user = new AdWordsUser(); // This code example shows how to run an AdWords API web application // while incorporating the OAuth2 installed application flow into your // application. If your application uses a single AdWords manager account // login to make calls to all your accounts, you shouldn't use this code // example. Instead, you should run OAuthTokenGenerator.exe to generate a // refresh token and use that configuration in your application's // App.config. AdWordsAppConfig config = user.Config as AdWordsAppConfig; if (user.Config.OAuth2Mode == OAuth2Flow.APPLICATION && string.IsNullOrEmpty(config.OAuth2RefreshToken)) { DoAuth2Authorization(user); } Console.Write("Enter the customer id: "); string customerId = Console.ReadLine(); config.ClientCustomerId = customerId; // Get the CampaignService. CampaignService campaignService = (CampaignService) user.GetService(AdWordsService.v201601.CampaignService); Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status }, ordering = new OrderBy[] { OrderBy.Asc(Campaign.Fields.Name) }, paging = Paging.Default }; CampaignPage page = new CampaignPage(); try { do { // Get the campaigns. page = campaignService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (Campaign campaign in page.entries) { Console.WriteLine("{0}) Campaign with id = '{1}', name = '{2}' and status = '{3}'" + " was found.", i + 1, campaign.id, campaign.name, campaign.status); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of campaigns found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to retrieve campaigns", e); } }
/// <summary> /// Handles the Click event of the btnGetCampaigns control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing /// the event data.</param> protected void OnGetCampaignsButtonClick(object sender, EventArgs eventArgs) { ConfigureUserForOAuth(); // Now proceed to make your API calls as usual. // Create a selector. Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Name, Campaign.Fields.Status }, ordering = new OrderBy[] { OrderBy.Asc(Campaign.Fields.Name) } }; (user.Config as AdWordsAppConfig).ClientCustomerId = txtCustomerId.Text; try { CampaignService service = (CampaignService)user.GetService(AdWordsService.v201802.CampaignService); CampaignPage page = service.get(selector); // Display campaigns. if (page != null && page.entries != null && page.entries.Length > 0) { DataTable dataTable = new DataTable(); dataTable.Columns.AddRange(new DataColumn[] { new DataColumn("Serial No.", typeof(int)), new DataColumn("Campaign Id", typeof(long)), new DataColumn("Campaign Name", typeof(string)), new DataColumn("Status", typeof(string)) }); for (int i = 0; i < page.entries.Length; i++) { Campaign campaign = page.entries[i]; DataRow dataRow = dataTable.NewRow(); dataRow.ItemArray = new object[] { i + 1, campaign.id, campaign.name, campaign.status.ToString() }; dataTable.Rows.Add(dataRow); } CampaignGrid.DataSource = dataTable; CampaignGrid.DataBind(); } else { Response.Write("No campaigns were found."); } } catch (Exception e) { Response.Write(string.Format("Failed to get campaigns. Exception says \"{0}\"", e.Message)); } }