2013-01-14 63 views
3

我已經嘗試運行的C#示例代碼,我收到以下錯誤:的Bing Ads API的C#示例代碼不工作

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

我也試圖實現在自己的代碼報告服務,但已收到

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

response = reportingService.SubmitGenerateReport(request); 

這裏是我的代碼,刪除的ID /密碼/令牌:

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) 
     { 
     } 
    } 
} 

我被困在如何從API獲取報告,任何幫助將不勝感激。

UPDATE:聽起來很傻,但它是一個不正確競選ID傳入,感謝您的幫助所有。

+0

我對這段代碼一無所知,但從經驗來看,如果你不能運行一個示例程序,那麼這個項目的設置很可能有問題。就像缺少一個導入或其他東西。 –

+0

你是正確的,這是一個不正確的競選編號 –

回答

1

你的代碼告訴你爲什麼你得到這個錯誤;

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

您試圖訪問一個null值的SubmitGenerateReport方法。你不能那樣做。你應該首先初始化它。

+0

我會同意你的,但兩件事1.我試圖初始化它,並得到了同樣的錯誤2.這是微軟如何在示例代碼︰http:// msdn.microsoft.com/en-US/library/bb671680.aspx –

+0

@GregBurris我認爲這個例子使用Reporting服務或其他東西。也許你需要在visual studio中再添加一些設置選項。 –

+0

你是對的,它使用微軟的服務。它可以使用Visual Studio中的「服務參考」來添加,目標地址是:https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl –

0

傳入錯誤的數據。我希望錯誤信息會更具描述性,而不僅僅是內部錯誤。