/// <summary> /// Get a specific custom channel from the host AdSense account. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/customchannels/get /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="adClientId">Ad client from which to get the custom channel.</param> /// <param name="customChannelId">Custom channel to get.</param> /// <returns>CustomChannelResponse</returns> public static CustomChannel Get(AdSenseHostService service, string adClientId, string customChannelId) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (adClientId == null) { throw new ArgumentNullException(adClientId); } if (customChannelId == null) { throw new ArgumentNullException(customChannelId); } // Make the request. return(service.Customchannels.Get(adClientId, customChannelId).Execute()); } catch (Exception ex) { throw new Exception("Request Customchannels.Get failed.", ex); } }
/// <summary> /// Generate an AdSense report based on the report request sent in the query parameters. Returns the result as JSON; to retrieve output in CSV format specify "alt=csv" as a query parameter. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/reports/generate /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="startDate">Start of the date range to report on in "YYYY-MM-DD" format, inclusive.</param> /// <param name="endDate">End of the date range to report on in "YYYY-MM-DD" format, inclusive.</param> /// <param name="optional">Optional paramaters.</param> /// <returns>ReportResponse</returns> public static Report Generate(AdSenseHostService service, string startDate, string endDate, ReportsGenerateOptionalParms optional = null) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (startDate == null) { throw new ArgumentNullException(startDate); } if (endDate == null) { throw new ArgumentNullException(endDate); } // Building the initial request. var request = service.Reports.Generate(startDate, endDate); // Applying optional parameters to the request. request = (ReportsResource.GenerateRequest)SampleHelpers.ApplyOptionalParms(request, optional); // Requesting data. return(request.Execute()); } catch (Exception ex) { throw new Exception("Request Reports.Generate failed.", ex); } }
static void Main(string[] args) { Console.WriteLine("AdSenseHost sample"); Console.WriteLine("=================="); GoogleWebAuthorizationBroker.Folder = "AdSenseHost.Sample"; UserCredential credential; using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) { credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, new[] { AdSenseHostService.Scope.Adsensehost }, "user", CancellationToken.None, new FileDataStore("AdSenseHostSampleStore")).Result; } // Create the service. var service = new AdSenseHostService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "AdSense API Sample", }); // Execute Host calls HostApiConsumer hostApiConsumer = new HostApiConsumer(service, MaxListPageSize); hostApiConsumer.RunCalls(); // Execute Publisher calls PublisherApiConsumer publisherApiConsumer = new PublisherApiConsumer(service, MaxListPageSize); publisherApiConsumer.RunCalls(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
/// <summary> /// Add a new URL channel to the host AdSense account. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/urlchannels/insert /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="adClientId">Ad client to which the new URL channel will be added.</param> /// <param name="body">A valid AdSenseHost v4.1 body.</param> /// <returns>UrlChannelResponse</returns> public static UrlChannel Insert(AdSenseHostService service, string adClientId, UrlChannel body) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (body == null) { throw new ArgumentNullException("body"); } if (adClientId == null) { throw new ArgumentNullException(adClientId); } // Make the request. return(service.Urlchannels.Insert(body, adClientId).Execute()); } catch (Exception ex) { throw new Exception("Request Urlchannels.Insert failed.", ex); } }
/// <summary> /// Delete the specified ad unit from the specified publisher AdSense account. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/adunits/delete /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="accountId">Account which contains the ad unit.</param> /// <param name="adClientId">Ad client for which to get ad unit.</param> /// <param name="adUnitId">Ad unit to delete.</param> /// <returns>AdUnitResponse</returns> public static AdUnit Delete(AdSenseHostService service, string accountId, string adClientId, string adUnitId) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (accountId == null) { throw new ArgumentNullException(accountId); } if (adClientId == null) { throw new ArgumentNullException(adClientId); } if (adUnitId == null) { throw new ArgumentNullException(adUnitId); } // Make the request. return(service.Adunits.Delete(accountId, adClientId, adUnitId).Execute()); } catch (Exception ex) { throw new Exception("Request Adunits.Delete failed.", ex); } }
/// <summary> /// Create an association session for initiating an association with an AdSense user. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/associationsessions/start /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="productCode">Products to associate with the user.</param> /// <param name="websiteUrl">The URL of the user's hosted website.</param> /// <param name="optional">Optional paramaters.</param> /// <returns>AssociationSessionResponse</returns> public static AssociationSession Start(AdSenseHostService service, string productCode, string websiteUrl, AssociationsessionsStartOptionalParms optional = null) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (productCode == null) { throw new ArgumentNullException(productCode); } if (websiteUrl == null) { throw new ArgumentNullException(websiteUrl); } // Building the initial request. var request = service.Associationsessions.Start(productCode, websiteUrl); // Applying optional parameters to the request. request = (AssociationsessionsResource.StartRequest)SampleHelpers.ApplyOptionalParms(request, optional); // Requesting data. return(request.Execute()); } catch (Exception ex) { throw new Exception("Request Associationsessions.Start failed.", ex); } }
/// <summary> /// List all host custom channels in this AdSense account. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/customchannels/list /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="adClientId">Ad client for which to list custom channels.</param> /// <param name="optional">Optional paramaters.</param> /// <returns>CustomChannelsResponse</returns> public static CustomChannels List(AdSenseHostService service, string adClientId, CustomchannelsListOptionalParms optional = null) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (adClientId == null) { throw new ArgumentNullException(adClientId); } // Building the initial request. var request = service.Customchannels.List(adClientId); // Applying optional parameters to the request. request = (CustomchannelsResource.ListRequest)SampleHelpers.ApplyOptionalParms(request, optional); // Requesting data. return(request.Execute()); } catch (Exception ex) { throw new Exception("Request Customchannels.List failed.", ex); } }
/// <summary> /// This example verifies an association session callback token. /// </summary> /// <param name="adsense">AdSensehost service object on which to run the requests.</param> /// <param name="callbackToken">The token returned from the association callback.</param> public static void VerifyAssociationSession(AdSenseHostService adsense, string callbackToken) { Console.WriteLine("================================================================="); Console.WriteLine("Verifying new association session"); Console.WriteLine("================================================================="); // Verify the association session token. AssociationSession associationSession = adsense.Associationsessions.Verify(callbackToken) .Execute(); Console.WriteLine("Association for account {0} has status {1} and ID {2}.", associationSession.AccountId, associationSession.Status, associationSession.Id); Console.WriteLine(); }
/// <summary> /// This example prints a report, using a filter for a specified ad client. /// </summary> /// <param name="service">AdSense service object on which to run the requests.</param> /// <param name="adClientId">The ID for the ad client to be used.</param> private void GenerateReport(AdSenseHostService service, string adClientId) { Console.WriteLine("================================================================="); Console.WriteLine("Running report for ad client {0}", adClientId); Console.WriteLine("================================================================="); // Prepare report. var startDate = DateTime.Today.ToString(DateFormat); var endDate = DateTime.Today.AddDays(-7).ToString(DateFormat); ReportsResource.GenerateRequest reportRequest = this.service.Reports.Generate(startDate, endDate); // Specify the desired ad client using a filter, as well as other parameters. // A complete list of metrics and dimensions is available on the documentation. reportRequest.Filter = new List <string> { "AD_CLIENT_ID==" + ReportHelper.EscapeFilterParameter(adClientId) }; reportRequest.Metric = new List <string> { "PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS" }; reportRequest.Dimension = new List <string> { "DATE" }; //A list of dimensions to sort by: + means ascending, - means descending reportRequest.Sort = new List <string> { "+DATE" }; // Run report. Report reportResponse = reportRequest.Execute(); if (reportResponse.Rows != null && reportResponse.Rows.Count > 0) { ReportHelper.displayHeaders(reportResponse.Headers); ReportHelper.displayRows(reportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }
/// <summary>This example starts an association session.</summary> /// <param name="adsense">AdSensehost service object on which to run the requests.</param> /// <param name="websiteUrl">The URL of the user's hosted website.</param> /// <returns>The created association.</returns> public static AssociationSession StartAssociationSession(AdSenseHostService adsense, string websiteUrl) { Console.WriteLine("================================================================="); Console.WriteLine("Creating new association session"); Console.WriteLine("================================================================="); // Request a new association session. AssociationSession associationSession = adsense.Associationsessions.Start( AssociationsessionsResource.StartRequest.ProductCodeEnum.AFC, websiteUrl).Execute(); Console.WriteLine("Association with ID {0} and redirect URL \n{1}\n was started.", associationSession.Id, associationSession.RedirectUrl); Console.WriteLine(); // Return the Association Session that was just created. return associationSession; }
/// <summary>This example starts an association session.</summary> /// <param name="adsense">AdSensehost service object on which to run the requests.</param> /// <param name="websiteUrl">The URL of the user's hosted website.</param> /// <returns>The created association.</returns> public static AssociationSession StartAssociationSession(AdSenseHostService adsense, string websiteUrl) { Console.WriteLine("================================================================="); Console.WriteLine("Creating new association session"); Console.WriteLine("================================================================="); // Request a new association session. AssociationSession associationSession = adsense.Associationsessions.Start( AssociationsessionsResource.StartRequest.ProductCodeEnum.AFC, websiteUrl).Execute(); Console.WriteLine("Association with ID {0} and redirect URL \n{1}\n was started.", associationSession.Id, associationSession.RedirectUrl); Console.WriteLine(); // Return the Association Session that was just created. return(associationSession); }
/// <summary> /// Verify an association session after the association callback returns from AdSense signup. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/associationsessions/verify /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="token">The token returned to the association callback URL.</param> /// <returns>AssociationSessionResponse</returns> public static AssociationSession Verify(AdSenseHostService service, string token) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (token == null) { throw new ArgumentNullException(token); } // Make the request. return(service.Associationsessions.Verify(token).Execute()); } catch (Exception ex) { throw new Exception("Request Associationsessions.Verify failed.", ex); } }
public static void Main(string[] args) { Console.WriteLine("AdSense Host API Command Line Sample - Association sessions"); GoogleWebAuthorizationBroker.Folder = "AdSenseHost.Sample"; UserCredential credential; using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) { credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, new[] { AdSenseHostService.Scope.Adsensehost }, "user", CancellationToken.None, new FileDataStore("AdSenseHostSampleStore")).Result; } // Create the service. var service = new AdSenseHostService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "AdSense API Sample", }); Console.WriteLine("Insert website URL"); string websiteUrl = Console.ReadLine(); /* 1. Create the association session. */ StartAssociationSession(service, websiteUrl); /* 2. Use the token to verify the association. */ Console.WriteLine("Insert callback token"); string callbackToken = Console.ReadLine(); VerifyAssociationSession(service, callbackToken); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
/// <summary> /// Get ad code for the specified ad unit, attaching the specified host custom channels. /// Documentation https://developers.google.com/adsensehost/v4.1/reference/adunits/getAdCode /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated AdSenseHost service.</param> /// <param name="accountId">Account which contains the ad client.</param> /// <param name="adClientId">Ad client with contains the ad unit.</param> /// <param name="adUnitId">Ad unit to get the code for.</param> /// <param name="optional">Optional paramaters.</param> /// <returns>AdCodeResponse</returns> public static AdCode GetAdCode(AdSenseHostService service, string accountId, string adClientId, string adUnitId, AdunitsGetAdCodeOptionalParms optional = null) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (accountId == null) { throw new ArgumentNullException(accountId); } if (adClientId == null) { throw new ArgumentNullException(adClientId); } if (adUnitId == null) { throw new ArgumentNullException(adUnitId); } // Building the initial request. var request = service.Adunits.GetAdCode(accountId, adClientId, adUnitId); // Applying optional parameters to the request. request = (AdunitsResource.GetAdCodeRequest)SampleHelpers.ApplyOptionalParms(request, optional); // Requesting data. return(request.Execute()); } catch (Exception ex) { throw new Exception("Request Adunits.GetAdCode failed.", ex); } }
/// <summary> /// Runs multiple Host requests againt the AdSense Host API. /// </summary> /// <param name="service">AdSensehost service object on which to run the requests.</param> /// <param name="maxListPageSize">The maximum page size to retrieve.</param> public HostApiConsumer(AdSenseHostService service, int maxListPageSize) { this.service = service; this.maxListPageSize = maxListPageSize; }
/// <summary> /// Runs multiple Publisher requests against the AdSense Host API. /// </summary> /// <param name="service">AdSensehost service object on which to run the requests.</param> /// <param name="maxListPageSize">The maximum page size to retrieve.</param> public PublisherApiConsumer(AdSenseHostService service, int maxListPageSize) { this.service = service; this.maxListPageSize = maxListPageSize; }
/// <summary> /// This example prints a report, using a filter for a specified ad client. /// </summary> /// <param name="service">AdSense service object on which to run the requests.</param> /// <param name="adClientId">The ID for the ad client to be used.</param> private void GenerateReport(AdSenseHostService service, string adClientId) { Console.WriteLine("================================================================="); Console.WriteLine("Running report for ad client {0}", adClientId); Console.WriteLine("================================================================="); // Prepare report. var startDate = DateTime.Today.ToString(DateFormat); var endDate = DateTime.Today.AddDays(-7).ToString(DateFormat); ReportsResource.GenerateRequest reportRequest = this.service.Reports.Generate(startDate, endDate); // Specify the desired ad client using a filter, as well as other parameters. // A complete list of metrics and dimensions is available on the documentation. reportRequest.Filter = new List<string> { "AD_CLIENT_ID==" + ReportHelper.EscapeFilterParameter(adClientId) }; reportRequest.Metric = new List<string> { "PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS" }; reportRequest.Dimension = new List<string> { "DATE" }; //A list of dimensions to sort by: + means ascending, - means descending reportRequest.Sort = new List<string> { "+DATE" }; // Run report. Report reportResponse = reportRequest.Execute(); if (reportResponse.Rows != null && reportResponse.Rows.Count > 0) { ReportHelper.displayHeaders(reportResponse.Headers); ReportHelper.displayRows(reportResponse.Rows); } else { Console.WriteLine("No rows returned."); } Console.WriteLine(); }