报告请求和下载失败 [英] Report Request and Download Failure

查看:95
本文介绍了报告请求和下载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过请求并从沙箱广告帐户下载报告,但每次出现系统内部错误。


我已经请求并使用下面提到的代码下载报告。

 public static void GetKeywordPerformanceReport(
string username,
string password,
string appToken,
string devToken,
string customerId,
int [] accountIds,
string zipFileName)
{
//创建并初始化关键字效果报告请求
// object。
KeywordPerformanceReportRequest request =
new KeywordPerformanceReportRequest();

//指定报告的语言。
request.Language = ReportLanguage.English;

//指定报告的格式。
request.Format = ReportFormat.Xml;
request.ReturnOnlyCompleteData = false;
request.ReportName =" My Keyword Report" ;;
request.Aggregation = ReportAggregation.Monthly;

//指定报告的时间范围。
#if(USE_PREDEFINED_TIME)
request.Time = new ReportTime();
request.Time.PredefinedTime = ReportTimePeriod.LastSixMonths;
request.Time.CustomDates = null;
request.Time.CustomDateRangeStart = null;
request.Time.CustomDateRangeEnd = null;

#elif(USE_DATE_RANGE)
//创建ReportTime类的实例以保存报告日期
//信息。
request.Time = new ReportTime();

//将报告的开始日期设置为今天之前的一个月。
DateTime startDate = DateTime.Today.AddMonths(-1);
request.Time.CustomDateRangeStart = new Date();
request.Time.CustomDateRangeStart.Day = startDate.Day;
request.Time.CustomDateRangeStart.Month = startDate.Month;
request.Time.CustomDateRangeStart.Year = startDate.Year;

//将结束日期设置为今天。
DateTime endDate = DateTime.Today;
request.Time.CustomDateRangeEnd = new Date();
request.Time.CustomDateRangeEnd.Day = endDate.Day;
request.Time.CustomDateRangeEnd.Month = endDate.Month;
request.Time.CustomDateRangeEnd.Year = endDate.Year;

#elif(USE_CUSTOM_DATES)
//创建ReportTime类的实例以保存报告日期
//信息。
request.Time = new ReportTime();

//分配一组DateTime结构来保存报告日期。
request.Time.CustomDates = new Date [2];

//创建2005年12月28日的日期。
DateTime tempDate = new DateTime(2005,12,28);
request.Time.CustomDates [0] = new Date();
request.Time.CustomDates [0] .Day = tempDate.Day;
request.Time.CustomDates [0] .Month = tempDate.Month;
request.Time.CustomDates [0] .Year = tempDate.Year;

//在今天之前创建一个月的日期。
tempDate = DateTime.Today.AddMonths(-1);
request.Time.CustomDates [1] = new Date();
request.Time.CustomDates [1] .Day = tempDate.Day;
request.Time.CustomDates [1] .Month = tempDate.Month;
request.Time.CustomDates [1] .Year = tempDate.Year;
#endif

//指定将在报告中的列。
request.Columns = new KeywordPerformanceReportColumn [7];
request.Columns [0] = KeywordPerformanceReportColumn.AccountName;
request.Columns [1] = KeywordPerformanceReportColumn.CampaignName;
request.Columns [2] = KeywordPerformanceReportColumn.Keyword;
request.Columns [3] = KeywordPerformanceReportColumn.TimePeriod;
request.Columns [4] = KeywordPerformanceReportColumn.Impressions;
request.Columns [5] = KeywordPerformanceReportColumn.Conversions;
request.Columns [6] = KeywordPerformanceReportColumn.AdId;

//指定报告的范围。此示例仅向帐户级别下调
//,但您可以请求任何
//帐户,广告组和广告系列的报告。
request.Scope = new AccountThroughAdGroupReportScope();
request.Scope.AccountIds = accountIds;
request.Scope.AdGroups = null;
request.Scope.Campaigns = null;

//指定报告的过滤器。此示例请求
//仅搜索在美国显示的广告在
//报表中。
request.Filter = new KeywordPerformanceReportFilter();
request.Filter.AdDistribution = AdDistributionReportFilter.Search;
request.Filter.LanguageAndRegion =
LanguageAndRegionReportFilter.UnitedStates;
request.Filter.DeliveredMatchType = null;
request.Filter.Keywords = null;

//创建并初始化ReportingServiceClient对象。
ReportingServiceClient服务= new ReportingServiceClient();

//提交报告请求。
try
{
//创建并初始化QueueReportRequest对象。
SubmitGenerateReportRequest submitRequest =
new SubmitGenerateReportRequest();
submitRequest.ApplicationToken = appToken;
submitRequest.DeveloperToken = devToken;
submitRequest.UserName = username;
submitRequest.Password = password;
submitRequest.ReportRequest = request;

//提交报告请求。如果
//发生错误,这将抛出异常。
SubmitGenerateReportResponse queueResponse;
queueResponse = service.SubmitGenerateReport(submitRequest);

//轮询以获取报告的状态,直到报告完成为止。
int waitMinutes = 15;
int maxWaitMinutes = 120;
DateTime startTime = DateTime.Now;
int elapsedMinutes = 0;

//将GetReportStatusResponse对象初始化为
中的错误//发生错误。错误将在下面处理。
PollGenerateReportResponse pollResponse = null;
do
{
//在轮询前等待指定的分钟数。
System.Threading.Thread.Sleep(waitMinutes * 60 * 1000);
elapsedMinutes = DateTime.Now.Subtract(startTime).Minutes;

//获取报告的状态。
pollResponse = CheckReportStatus(
用户名,
密码,
appToken,
devToken,
customerId,
服务,
queueResponse。 ReportRequestId);

if(ReportRequestStatusType.Success ==
pollResponse.ReportRequestStatus.Status)
{
//报告准备就绪。
休息;
}
else if(ReportRequestStatusType.Pending ==
pollResponse.ReportRequestStatus.Status)
{
//报告尚未准备好。
继续;
}
else
{
//发生错误。
休息;
}
} while(elapsedMinutes< maxWaitMinutes);

//如果报告已创建,请下载。
if((null!= pollResponse)&&
(ReportRequestStatusType.Success ==
pollResponse.ReportRequestStatus.Status))
{
//下载文件。
DownloadReport(
pollResponse.ReportRequestStatus.ReportDownloadUrl,
zipFileName);
}

}

catch(FaultException< AdApiFaultDetail> fault)
{
AdApiFaultDetail faultDetail = fault.Detail;

//编写API错误。
foreach(faultDetail.Errors中的AdApiError opError)
{
Console.WriteLine(
String.Format(" Error {0}:",opError.Code));
Console.WriteLine(
String.Format(" \tMessage:\" {0} \"",opError.Message));
}

}

catch(FaultException< ApiFaultDetail> fault)
{
ApiFaultDetail faultDetail = fault.Detail;

//显示服务操作错误信息。
foreach(faultDetail.OperationErrors中的OperationError opError)
{
Console.Write(" Operation error");
Console.WriteLine("''{0}'({1})遇到。",
opError.Message,
opError.Code);
}

//显示批量错误信息。
foreach(faultDetail.BatchErrors中的BatchError batchError)
{
Console.Write(" Batch error");
Console.Write("''{0}'({1})遇到',
batchError.Message,
batchError.ErrorCode);
}

}

//在客户端捕获与
// adCenter API无关的例外情况。一个例子是客户端上的
//内存不足情况。
catch(例外e)
{
Console.WriteLine(遇到错误'{0}'。',
e.Message);
}
最后
{
//确保关闭服务。
service.Close();
}
}

public static PollGenerateReportResponse CheckReportStatus(
string username,
string password,
string appToken,
string devToken ,
string customerId,
ReportingServiceClient服务,
string reportId)
{
PollGenerateReportRequest pollRequest =
new PollGenerateReportRequest();
pollRequest.ApplicationToken = null;
pollRequest.DeveloperToken = devToken;
pollRequest.UserName = username;
pollRequest.Password =密码;
pollRequest.ReportRequestId = reportId;

//获取报告的状态。
返回service.PollGenerateReport(pollRequest);
}

public static void DownloadReport(
string downloadUrl,
string zipFileName)
{
//打开到URL的连接该报告可用。
HttpWebRequest webRequest =
(HttpWebRequest)WebRequest.Create(downloadUrl);
HttpWebResponse response =
(HttpWebResponse)webRequest.GetResponse();
Stream httpStream = response.GetResponseStream();

//打开报告文件。
FileInfo zipFileInfo = new FileInfo(zipFileName);
if(!zipFileInfo.Directory.Exists)
{
zipFileInfo.Directory.Create();
}
FileStream fileStream = new FileStream(
zipFileInfo.FullName,
FileMode.Create);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);
BinaryReader binaryReader = new BinaryReader(httpStream);
try
{
//阅读报告并将其保存到文件中。
int bufferSize = 100000;
while(true)
{
//从API读取报告数据。
byte [] buffer = binaryReader.ReadBytes(bufferSize);

//将报告数据写入文件。
binaryWriter.Write(buffer);

//如果到达报告的结尾,则跳出
//循环。
if(buffer.Length!= bufferSize)
{
break;
}
}
}
最后
{
//清理。
binaryWriter.Close();
binaryReader.Close();
fileStream.Close();
httpStream.Close();
}
}


 


所以,请指导我如何从沙盒adcenter帐户请求和下载报告。

解决方案

您的代码看起来正确。更重要的是,我们需要的是您传递的SOAP,其中包含所有参数。 这里有关于捕获SOAP的简短视频:


http://community.microsoftadvertising.com/blogs/api/archive/2009/01/08/soap-capture-training-video.aspx



如果您仍然遇到问题,请打开支持票


此致,


Jeff Dillon,API支撑


I have tried to request and download a report from sandbox adcenter account but every time system internal error occurs.

I have request and download a report using below mention code.

public static void GetKeywordPerformanceReport(
    string username,
    string password,
    string appToken,
    string devToken,
    string customerId,
    int[] accountIds,
    string zipFileName)
{
    // Create and initialize the keyword performance report request 
    // object.
    KeywordPerformanceReportRequest request =
        new KeywordPerformanceReportRequest();

    // Specify the language for the report.
    request.Language = ReportLanguage.English;

    // Specify the format of the report.
    request.Format = ReportFormat.Xml;
    request.ReturnOnlyCompleteData = false;
    request.ReportName = "My Keyword Report";
    request.Aggregation = ReportAggregation.Monthly;

    // Specify the timeframe of the report.
#if(USE_PREDEFINED_TIME)
    request.Time = new ReportTime();
    request.Time.PredefinedTime = ReportTimePeriod.LastSixMonths;
    request.Time.CustomDates = null;
    request.Time.CustomDateRangeStart = null;
    request.Time.CustomDateRangeEnd = null;

#elif(USE_DATE_RANGE)
    // Create an instance of the ReportTime class to hold the report date 
    // information.
    request.Time = new ReportTime();

    // Set the start date for the report to one month before today.
    DateTime startDate = DateTime.Today.AddMonths(-1);
    request.Time.CustomDateRangeStart = new Date();
    request.Time.CustomDateRangeStart.Day = startDate.Day;
    request.Time.CustomDateRangeStart.Month = startDate.Month;
    request.Time.CustomDateRangeStart.Year = startDate.Year;

    // Set the end date to today.
    DateTime endDate = DateTime.Today;
    request.Time.CustomDateRangeEnd = new Date();
    request.Time.CustomDateRangeEnd.Day = endDate.Day;
    request.Time.CustomDateRangeEnd.Month = endDate.Month;
    request.Time.CustomDateRangeEnd.Year = endDate.Year;

#elif(USE_CUSTOM_DATES)
    // Create an instance of the ReportTime class to hold the report date
    // information.
    request.Time = new ReportTime();

    // Allocate an array of DateTime structures to hold the report dates.
    request.Time.CustomDates = new Date[2];

    // Create a date for 12/28/2005.
    DateTime tempDate = new DateTime(2005, 12, 28);
    request.Time.CustomDates[0] = new Date();
    request.Time.CustomDates[0].Day = tempDate.Day;
    request.Time.CustomDates[0].Month = tempDate.Month;
    request.Time.CustomDates[0].Year = tempDate.Year;

    // Create a date for one month before today.
    tempDate = DateTime.Today.AddMonths(-1);
    request.Time.CustomDates[1] = new Date();
    request.Time.CustomDates[1].Day = tempDate.Day;
    request.Time.CustomDates[1].Month = tempDate.Month;
    request.Time.CustomDates[1].Year = tempDate.Year;
#endif

    // Specify the columns that will be in the report.
    request.Columns = new KeywordPerformanceReportColumn[7];
    request.Columns[0] = KeywordPerformanceReportColumn.AccountName;
    request.Columns[1] = KeywordPerformanceReportColumn.CampaignName;
    request.Columns[2] = KeywordPerformanceReportColumn.Keyword;
    request.Columns[3] = KeywordPerformanceReportColumn.TimePeriod;
    request.Columns[4] = KeywordPerformanceReportColumn.Impressions;
    request.Columns[5] = KeywordPerformanceReportColumn.Conversions;
    request.Columns[6] = KeywordPerformanceReportColumn.AdId;

    // Specify the scope of the report. This example goes down 
    // only to the account level, but you can request a report for any 
    // number of accounts, ad groups, and campaigns.
    request.Scope = new AccountThroughAdGroupReportScope();
    request.Scope.AccountIds = accountIds;
    request.Scope.AdGroups = null;
    request.Scope.Campaigns = null;

    // Specify the filter for the report. This example requests 
    // only search ads that were displayed in the United States to be in 
    // the report.
    request.Filter = new KeywordPerformanceReportFilter();
    request.Filter.AdDistribution = AdDistributionReportFilter.Search;
    request.Filter.LanguageAndRegion =
        LanguageAndRegionReportFilter.UnitedStates;
    request.Filter.DeliveredMatchType = null;
    request.Filter.Keywords = null;

    // Create and initialize the ReportingServiceClient object.
    ReportingServiceClient service = new ReportingServiceClient();

    // Submit the report request.
    try
    {
        // Create and initialize the QueueReportRequest object.
        SubmitGenerateReportRequest submitRequest = 
            new SubmitGenerateReportRequest();
        submitRequest.ApplicationToken = appToken;
        submitRequest.DeveloperToken = devToken;
        submitRequest.UserName = username;
        submitRequest.Password = password;
        submitRequest.ReportRequest = request;

        // Submit the report request. This will throw an exception if 
        // an error occurs.
        SubmitGenerateReportResponse queueResponse;
        queueResponse = service.SubmitGenerateReport(submitRequest);

        // Poll to get the status of the report until it is complete.
        int waitMinutes = 15;
        int maxWaitMinutes = 120;
        DateTime startTime = DateTime.Now;
        int elapsedMinutes = 0;

        // Initialize the GetReportStatusResponse object to an error in 
        // case an error occurs. The error will be handled below.
        PollGenerateReportResponse pollResponse = null;
        do
        {
            // Wait the specified number of minutes before polling.
            System.Threading.Thread.Sleep(waitMinutes * 60 * 1000);
            elapsedMinutes = DateTime.Now.Subtract(startTime).Minutes;

            // Get the status of the report.
            pollResponse = CheckReportStatus(
                username, 
                password, 
                appToken, 
                devToken, 
                customerId, 
                service, 
                queueResponse.ReportRequestId);

            if (ReportRequestStatusType.Success ==
                pollResponse.ReportRequestStatus.Status)
            {
                // The report is ready.
                break;
            }
            else if (ReportRequestStatusType.Pending ==
                pollResponse.ReportRequestStatus.Status)
            {
                // The report is not ready yet.
                continue;
            }
            else
            {
                // An error occurred.
                break;
            }
        } while (elapsedMinutes < maxWaitMinutes);

        // If the report was created, download it.
        if ((null != pollResponse) &&
            (ReportRequestStatusType.Success ==
            pollResponse.ReportRequestStatus.Status))
        {
            // Download the file.
            DownloadReport(
                pollResponse.ReportRequestStatus.ReportDownloadUrl, 
                zipFileName);
        }

    }

    catch (FaultException<AdApiFaultDetail> fault)
    {
        AdApiFaultDetail faultDetail = fault.Detail;

        // Write the API errors.
        foreach (AdApiError opError in faultDetail.Errors)
        {
            Console.WriteLine(
                String.Format("Error {0}:", opError.Code));
            Console.WriteLine(
                String.Format("\tMessage: \"{0}\"", opError.Message));
        }

    }
    
    catch (FaultException<ApiFaultDetail> fault)
    {
        ApiFaultDetail faultDetail = fault.Detail;

        // Display service operation error information.
        foreach (OperationError opError in faultDetail.OperationErrors)
        {
            Console.Write("Operation error");
            Console.WriteLine(" '{0}' ({1}) encountered.",
                              opError.Message,
                              opError.Code);
        }

        // Display batch error information.
        foreach (BatchError batchError in faultDetail.BatchErrors)
        {
            Console.Write("Batch error");
            Console.Write(" '{0}' ({1}) encountered",
                           batchError.Message,
                           batchError.ErrorCode);
        }

    }

    // Capture exceptions on the client that are unrelated to
    // the adCenter API. An example would be an 
    // out-of-memory condition on the client.
    catch (Exception e)
    {
        Console.WriteLine("Error '{0}' encountered.",
            e.Message);
    }
    finally
    {
        // Make sure you close the service.
        service.Close();
    }
}

public static PollGenerateReportResponse CheckReportStatus(
    string username,
    string password,
    string appToken,
    string devToken,
    string customerId,
    ReportingServiceClient service,
    string reportId)
{
    PollGenerateReportRequest pollRequest =
        new PollGenerateReportRequest();
    pollRequest.ApplicationToken = null;
    pollRequest.DeveloperToken = devToken;
    pollRequest.UserName = username;
    pollRequest.Password = password;
    pollRequest.ReportRequestId = reportId;

    // Get the status of the report.
    return service.PollGenerateReport(pollRequest);
}

public static void DownloadReport(
    string downloadUrl,
    string zipFileName)
{
    // Open a connection to the URL where the report is available.
    HttpWebRequest webRequest =
        (HttpWebRequest)WebRequest.Create(downloadUrl);
    HttpWebResponse response =
        (HttpWebResponse)webRequest.GetResponse();
    Stream httpStream = response.GetResponseStream();

    // Open the report file.
    FileInfo zipFileInfo = new FileInfo(zipFileName);
    if (!zipFileInfo.Directory.Exists)
    {
        zipFileInfo.Directory.Create();
    }
    FileStream fileStream = new FileStream(
        zipFileInfo.FullName,
        FileMode.Create);
    BinaryWriter binaryWriter = new BinaryWriter(fileStream);
    BinaryReader binaryReader = new BinaryReader(httpStream);
    try
    {
        // Read the report and save it to the file.
        int bufferSize = 100000;
        while (true)
        {
            // Read report data from API.
            byte[] buffer = binaryReader.ReadBytes(bufferSize);

            // Write report data to file.
            binaryWriter.Write(buffer);

            // If the end of the report is reached, break out of the 
            // loop.
            if (buffer.Length != bufferSize)
            {
                break;
            }
        }
    }
    finally
    {
        // Clean up.
        binaryWriter.Close();
        binaryReader.Close();
        fileStream.Close();
        httpStream.Close();
    }
}

So, please guide me how to request and download a report from sandbox adcenter account.

解决方案

Your code looks correct. What more importantly what we need though is the SOAP that you are passing, which included all the parameters.  There is short video on capturing SOAP here:

http://community.microsoftadvertising.com/blogs/api/archive/2009/01/08/soap-capture-training-video.aspx

If you continue to have problems, please open a support ticket

Sincerely,

Jeff Dillon, API Support


这篇关于报告请求和下载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆