我正在尝试阅读/下载我使用Google Ad Manager Api https://developers.google.com/ad-manager/api/reporting在Google Ad Manager帐户中创建的一些报告
但无法获得结果
还尝试了该API的NetworkService和InventoryService服务,以从我的Google广告管理器帐户中抓取一些数据,这些数据可以正常运行,如下所示:
using Google.Api.Ads.AdManager.Lib;
using Google.Api.Ads.AdManager.Util.v201911;
using Google.Api.Ads.AdManager.v201911;
using Google.Api.Ads.Common.Util.Reports;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Google.Api.Ads.AdManager.Examples.CSharp.v201911
{
/// This example gets all ad units.
public class GetAllAdUnits : SampleBase
{
public string orderId;
/// Returns a description about the code example.
public string Description => "This example gets all ad units.";
/// Main method, to run this code example as a standalone application.
public static void Main()
{
GetAllAdUnits codeExample = new GetAllAdUnits();
Console.WriteLine(codeExample.Description);
try
{
codeExample.Run(new AdManagerUser());
}
catch (Exception e)
{
Console.WriteLine("Failed to get ad units. Exception says \"{0}\"", e.Message);
}
}
private string filePath = "/Users/fazeem/";
/// Run the code example.
public void Run(AdManagerUser user)
{
using (NetworkService networkService = user.GetService<NetworkService>())
{
var network = networkService.getCurrentNetwork();
Console.WriteLine(
$"Current network has the network code {network.networkCode} and " +
$"display name \"{network.displayName}\" and Currency {network.currencyCode}."
);
}
using (InventoryService inventoryService = user.GetService<InventoryService>())
{
// Create a statement to select ad units.
int pageSize = StatementBuilder.SUGGESTED_PAGE_LIMIT;
StatementBuilder statementBuilder1 =
new StatementBuilder().OrderBy("id ASC").Limit(pageSize);
// Retrieve a small amount of ad units at a time, paging through until all
// ad units have been retrieved.
int totalResultSetSize = 0;
do
{
AdUnitPage page =
inventoryService.getAdUnitsByStatement(statementBuilder1.ToStatement());
// Print out some information for each ad unit.
if (page.results != null)
{
totalResultSetSize = page.totalResultSetSize;
int i = page.startIndex;
foreach (AdUnit adUnit in page.results)
{
Console.WriteLine(
"{0}) Ad unit with ID \"{1}\" and name \"{2}\" was found.", i++,
adUnit.id, adUnit.name);
}
}
statementBuilder1.IncreaseOffsetBy(pageSize);
} while (statementBuilder1.GetOffset() < totalResultSetSize);
Console.WriteLine("Number of results found: {0}", totalResultSetSize);
}
/////////////////////////////////////////////////////////////////////
// Create report job.
/////////////////////////////////////////////////////////////////////
using (ReportService reportService = user.GetService<ReportService>())
{
ReportJob reportJob = new ReportJob();
reportJob.reportQuery = new ReportQuery();
reportJob.reportQuery.dimensions = new Dimension[]
{
Dimension.ORDER_ID,
Dimension.ORDER_NAME
};
reportJob.reportQuery.dimensionAttributes = new DimensionAttribute[]
{
DimensionAttribute.ORDER_TRAFFICKER,
DimensionAttribute.ORDER_START_DATE_TIME,
DimensionAttribute.ORDER_END_DATE_TIME
};
reportJob.reportQuery.columns = new Column[]
{
Column.AD_SERVER_IMPRESSIONS,
Column.AD_SERVER_CLICKS,
//Column.TOTAL_ACTIVE_VIEW_MEASURABLE_IMPRESSIONS,
//Column.AD_SERVER_CPM_AND_CPC_REVENUE,
//Column.AD_SERVER_WITHOUT_CPD_AVERAGE_ECPM
};
// Set a custom date range for the last 8 days
reportJob.reportQuery.dateRangeType = DateRangeType.CUSTOM_DATE;
System.DateTime endDateTime = System.DateTime.Now;
reportJob.reportQuery.startDate = DateTimeUtilities
.FromDateTime(endDateTime.AddDays(-8), "Australia/Sydney").date;
reportJob.reportQuery.endDate = DateTimeUtilities
.FromDateTime(endDateTime, "Australia/Sydney").date;
// Create statement object to filter for an order.
StatementBuilder statementBuilder = new StatementBuilder().Where("ORDER_ID = :id")
.AddValue("id", orderId);
reportJob.reportQuery.statement = statementBuilder.ToStatement();
// Run report job.
reportJob = reportService.runReportJob(reportJob);
// Download the report
ReportUtilities reportUtilities =
new ReportUtilities(new ReportService(), reportJob.id);
// Set download options.
ReportDownloadOptions options = new ReportDownloadOptions();
options.exportFormat = ExportFormat.CSV_DUMP;
options.useGzipCompression = true;
reportUtilities.reportDownloadOptions = options;
// Download the report.
using (ReportResponse reportResponse = reportUtilities.GetResponse())
{
reportResponse.Save(filePath);
}
Console.WriteLine("Report saved to \"{0}\".", filePath);
}
}
}
}
但是代码的reportService部分(用于下载/阅读报告)抛出此异常:“无法获取广告单元。异常显示为“ AdManagerApiException:类型为'Google.api.ads.adManager.lib的异常。 adManagerApiException'被抛出”
任何帮助/建议将不胜感激。 TIA
PS我最初是希望直接从Ad Manager帐户中读取一些报告,但找不到有关它的任何详细文档,所以最终请先下载然后再阅读它。因此,如果有人有正确的方法可以直接从中读取报告广告经理帐户,也请分享。