3

I have tried running the C# example code and I receive the following error:

Error code: InternalError <0>
Message: An internal error has occurred.
Detail: [this is blank]

I have also tried implementing the reporting service in my own code, but have received a

"Object reference not set to an instance of an object" error on this line:

response = reportingService.SubmitGenerateReport(request);

Here is my code, removed the ids/password/token:

using System;
using System.ServiceModel;
using System.Data;

// BingAds.CampaignManagement is the application-defined namespace of 
// the service reference used for this example. 

using collectBingAdsData.BingAds.Reporting;
using collectBingAdsData.BingAds.CampaignManagement;
using collectBingAdsData.BingAds.Bulk;

namespace collectBingAdsData
{
    class Program
    {
        private static CampaignManagementServiceClient campaignManagementService = null;
        private static ReportingServiceClient reportingService = null;
        private static BulkServiceClient bulkService = null;

        private static DataTable BingSemData;

        // Specify your credentials.

        private static string m_password = "password";
        private static string m_username = "username";
        private static string m_token = "token";

        // Specify the advertiser's account ID and customer ID.

        private static long customerID = 123123;
        private static long[] accountIDs = { 123123 };
        private static long[] campaignIDs = { 123123 };

        private static string m_downloadPath = @"c:\reports\keywordperf.zip";

        // Shows how to request a keyword performance report and download it
        // to the specified folder.
        //
        // args[0] - Report ID. To download an existing report request instead 
        //           of requesting a new report, set args[0] to a report ID.

        static void Main(string[] args)
        {
            getAccountsByCutomer(customerID);
            foreach (long accountID in accountIDs)
            {
                getCampaignsByAccount(accountID);
                foreach (long campaignID in campaignIDs)
                {
                    //getCampaignReport(accountID, campaignID);
                    getKeywordReport(accountID, campaignID);
                }
            }
        }

        private static void getKeywordReport(long accountID, long campaignID)
        {
            KeywordPerformanceReportRequest request = new KeywordPerformanceReportRequest();

            request = buildKeywordPerformanceRequest(accountID, campaignID);

            string BingData = RequestReport(request);
        }

        private static void getCampaignReport(long accountID, long campaignID)
        {
            CampaignPerformanceReportRequest request = new CampaignPerformanceReportRequest();

            request = buildCampaignPerformanceRequest(accountID, campaignID);

            string BingData = RequestReport(request);



        }

        // Request the report. Use the ID that the request returns to
        // check for the completion of the report.

        static string RequestReport(ReportRequest report)
        {
            SubmitGenerateReportRequest request = new SubmitGenerateReportRequest();
            SubmitGenerateReportResponse response = new SubmitGenerateReportResponse();

            try
            {
                // Set the header information.

                request.DeveloperToken = m_token;
                request.UserName = m_username;
                request.Password = m_password;

                // Set the request information.

                request.ReportRequest = report;

                response = reportingService.SubmitGenerateReport(request);
            }
            catch (FaultException<collectBingAdsData.BingAds.Reporting.AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("SubmitGenerateReport failed with the following faults:\n");

                foreach (collectBingAdsData.BingAds.Reporting.AdApiError error in fault.Detail.Errors)
                {
                    if (105 == error.Code) //  InvalidCredentials
                    {
                        Console.WriteLine("The specified credentials are not valid or the " +
                            "account is inactive.");
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                            error.ErrorCode, error.Code, error.Message, error.Detail);
                    }
                }
            }
            catch (FaultException<collectBingAdsData.BingAds.Reporting.ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("SubmitGenerateReport failed with the following faults:\n");

                foreach (collectBingAdsData.BingAds.Reporting.OperationError error in fault.Detail.OperationErrors)
                {
                    switch (error.Code)
                    {
                        case 106: //  UserIsNotAuthorized
                            Console.WriteLine("The user is not authorized to call this operation.");
                            break;

                        case 2004: // ReportingServiceNoCompleteDataAvaliable
                            Console.WriteLine("The report cannot request complete data because " +
                                "complete data is not available based on the specified " +
                                "aggregation, scope, and time period values.");
                            break;

                        case 2007: // ReportingServiceInvalidReportAggregation
                            Console.WriteLine("The requested aggregation is not valid for the " +
                                "specified reporting period.");
                            break;

                        case 2008: // ReportingServiceInvalidReportTimeSelection
                            Console.WriteLine("The report time period is not valid.");
                            break;

                        case 2009: // ReportingServiceInvalidCustomDateRangeStart
                            Console.WriteLine("The start date of the custom date range specified " +
                                "in the report time period is not valid.");
                            break;

                        case 2010: // ReportingServiceInvalidCustomDateRangeEnd
                            Console.WriteLine("The end date of the custom date range specified " +
                                "in the report time period is not valid.");
                            break;

                        case 2011: // ReportingServiceEndDateBeforeStartDate
                            Console.WriteLine("The end date of the custom date range specified " +
                                "in the report time period cannot be earlier than the start date.");
                            break;

                        case 2015: // ReportingServiceRequiredColumnsNotSelected
                            Console.WriteLine("The report request does not include all of the " +
                                "required columns.");
                            break;

                        case 2016: // ReportingServiceDuplicateColumns
                            Console.WriteLine("The list or report columns contains duplicate " +
                                "columns. Please ensure that there is only one {0} column.",
                                error.Details);
                            break;

                        case 2017: // ReportingServiceNoMeasureSelected
                            Console.WriteLine("The list or report columns must include one or " +
                                "more KPI columns such as Clicks and Impressions.");
                            break;

                        case 2028: // ReportingServiceInvalidAccountThruAdGroupReportScope
                            Console.WriteLine("The report scope cannot be null or empty.");
                            break;

                        case 2034: // ReportingServiceInvalidTimePeriodColumnForSummaryReport
                            Console.WriteLine("When the report aggregation type is summary, " +
                                "you cannot include TimePeriod as a report column.");
                            break;

                        default:
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

                foreach (collectBingAdsData.BingAds.Reporting.BatchError error in fault.Detail.BatchErrors)
                {
                    Console.WriteLine("Error code: {0} ({1})\nIndex: {2}\nMessage: {3}\nDetail: {4}\n",
                        error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
                }
            }

            return (null == response) ? null : response.ReportRequestId;
        }

        private static CampaignPerformanceReportRequest buildCampaignPerformanceRequest(long accountID, long campaignID)
        {
            CampaignPerformanceReportRequest request = new CampaignPerformanceReportRequest();
            request.Aggregation = ReportAggregation.Daily;

            request.Columns = new[] { 
                CampaignPerformanceReportColumn.AccountName, 
                CampaignPerformanceReportColumn.Impressions, 
                CampaignPerformanceReportColumn.Clicks, 
                CampaignPerformanceReportColumn.Conversions, 
                CampaignPerformanceReportColumn.ConversionRate
            };

            request.Format = ReportFormat.Csv;

            request.Scope = new AccountThroughCampaignReportScope()
            {
                AccountIds = accountIDs,
                Campaigns = new[] {
                    new CampaignReportScope {
                        CampaignId = campaignID,
                        ParentAccountId = accountID

                    }
                }
            };

            request.Time = new ReportTime()
            {
                PredefinedTime = ReportTimePeriod.Yesterday
            };
            return request;
        }

        private static KeywordPerformanceReportRequest buildKeywordPerformanceRequest(long accountID, long campaignID)
        {
            KeywordPerformanceReportRequest report = new KeywordPerformanceReportRequest();

            report.Format = ReportFormat.Xml;
            report.ReportName = "My Keyword Performance Report";
            report.ReturnOnlyCompleteData = true;
            report.Aggregation = ReportAggregation.Daily;

            report.Scope = new AccountThroughAdGroupReportScope()
            {
                AccountIds = null,
                AdGroups = null,
                Campaigns = new[] {
                    new CampaignReportScope {
                        CampaignId = accountID,
                        ParentAccountId = campaignID
                    }
                }
            };

            report.Time = new ReportTime()
            {
                //CustomDateRangeStart = new Date()
                //{
                //    Month = 2,
                //    Day = 1,
                //    Year = 2012
                //},
                //CustomDateRangeEnd = new Date()
                //{
                //    Month = 2,
                //    Day = 15,
                //    Year = 2012
                //},
                PredefinedTime = ReportTimePeriod.Yesterday
            };

            report.Filter = new KeywordPerformanceReportFilter()
            {
                DeviceType = DeviceTypeReportFilter.Computer |
                    DeviceTypeReportFilter.SmartPhone
            };

            report.Columns = new[] {
                    KeywordPerformanceReportColumn.TimePeriod,
                    KeywordPerformanceReportColumn.AccountId,
                    KeywordPerformanceReportColumn.CampaignId,
                    KeywordPerformanceReportColumn.Keyword,
                    KeywordPerformanceReportColumn.KeywordId,
                    KeywordPerformanceReportColumn.DeviceType,
                    KeywordPerformanceReportColumn.BidMatchType,
                    KeywordPerformanceReportColumn.Clicks,
                    KeywordPerformanceReportColumn.Impressions,
                    KeywordPerformanceReportColumn.Ctr,
                    KeywordPerformanceReportColumn.AverageCpc,
                    KeywordPerformanceReportColumn.Spend,
                    KeywordPerformanceReportColumn.QualityScore
                };

            return report;
        }

        private static void getCampaignsByAccount(long accountID)
        {
        }

        private static void getAccountsByCutomer(long customerID)
        {
        }
    }
}

I am stuck on how I can get a report from the API, any help would be appreciated.

UPDATE: Sounds silly, but it was a incorrect campaign id passed in, Thanks for the help all.

Greg Burris
  • 31
  • 1
  • 3
  • I don't know anything about this code, but from experience, if you can't run an example program, then there's most likely something wrong with how the project is set up. Like missing an import or something. – Jesse Jashinsky Jan 14 '13 at 17:10
  • you were correct, it was an incorrect campaign id – Greg Burris Jan 14 '13 at 22:02

2 Answers2

1

Your code tells you why you are getting this error;

private static ReportingServiceClient reportingService = null;
response = reportingService.SubmitGenerateReport(request);

You are trying to access the SubmitGenerateReport method of a null value. You can't do that. You should initialize it first.

Jesse Jashinsky
  • 10,313
  • 6
  • 38
  • 63
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • I would agree with you, but two things 1. I tried initializing it, and got the same error 2. that is how Microsoft has it in the example code: http://msdn.microsoft.com/en-US/library/bb671680.aspx – Greg Burris Jan 14 '13 at 17:14
  • @GregBurris I think this example use Reporting service or something. Maybe you need a few more setting option in visual studio. – Soner Gönül Jan 14 '13 at 17:25
  • You are correct it uses a Microsoft Service. It can be added using a "Service Reference" in Visual Studio targeting this address: https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl – Greg Burris Jan 14 '13 at 17:42
  • Thanks for the help Soner, it was an incorrect Campaign id passed in. DOH! – Greg Burris Jan 14 '13 at 22:03
0

Passed bad data in. I wish the Error message would have been a bit more descriptive instead of just internal error.

Greg Burris
  • 31
  • 1
  • 3