You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Bing Ads API与.NET Framework的C#控制台程序获广告系列数据求助

哥们,我完全懂你对着微软文档一脸懵的感觉,直接给你一步步能落地的操作,都是控制台里能跑的代码片段,跟着来就行:

获取指定时间段账户广告系列数据的简易步骤

第一步:安装必要的NuGet包

先在你的控制台项目里安装Bing Ads .NET SDK(这是微软广告API的官方封装,省得自己写底层请求),打开NuGet包管理器控制台,执行:

Install-Package BingAds

或者在NuGet可视化界面搜索"BingAds",安装最新稳定版即可。

第二步:初始化认证客户端

把你手里的clientID、DeveloperToken、RefreshToken等参数填进去,先搞定身份认证:

using Microsoft.BingAds;
using Microsoft.BingAds.Reporting;

// 替换成你的实际参数
var clientId = "你的ClientID";
var developerToken = "你的DeveloperToken";
var refreshToken = "你的RefreshToken";
var accountId = long.Parse("你的Account ID");
var customerId = long.Parse("你的CustomerID");

// 组装认证信息
var authorizationData = new AuthorizationData
{
    ClientId = clientId,
    DeveloperToken = developerToken,
    Authentication = new OAuthRefreshTokenCredential(clientId, refreshToken),
    AccountId = accountId,
    CustomerId = customerId
};

// 创建报告服务客户端
var reportingService = new ReportingServiceClient(authorizationData);

第三步:构建广告系列报告请求

指定报告类型(这里用CampaignPerformanceReport,专门拉取广告系列维度的数据),设置时间范围,选择你需要的字段:

// 定义你要查询的时间段,比如过去7天
var startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
var endDate = DateTime.Now.ToString("yyyy-MM-dd");

// 构建报告请求
var reportRequest = new CampaignPerformanceReportRequest
{
    Format = ReportFormat.Csv, // 选CSV格式,方便后续解析处理
    ReportName = $"Campaign_Report_{DateTime.Now:yyyyMMddHHmmss}",
    Aggregation = ReportAggregation.Daily, // 按天聚合数据,也可以选Weekly/Monthly等
    Time = new ReportTime
    {
        PredefinedTime = ReportTimePeriod.CustomDateRange,
        StartDate = startDate,
        EndDate = endDate
    },
    // 选择需要的字段,按需增减,比如加转化数、点击率等
    Columns = new[]
    {
        CampaignPerformanceReportColumn.AccountId,
        CampaignPerformanceReportColumn.CampaignId,
        CampaignPerformanceReportColumn.CampaignName,
        CampaignPerformanceReportColumn.Impressions,
        CampaignPerformanceReportColumn.Clicks,
        CampaignPerformanceReportColumn.Spend,
        CampaignPerformanceReportColumn.Date
    },
    // 可选:添加筛选,比如只拉取活跃的广告系列
    Filter = new CampaignPerformanceReportFilter
    {
        Status = ReportFilterStatus.Active
    }
};

第四步:提交请求并等待报告生成

报告不是即时生成的,需要提交后轮询状态,直到准备好:

// 提交报告请求,拿到报告ID
var reportId = reportingService.SubmitGenerateReport(reportRequest);

// 轮询报告状态,每5秒查一次
ReportRequestStatus reportStatus;
do
{
    reportStatus = reportingService.GetReportRequestStatus(reportId);
    Console.WriteLine($"当前报告状态:{reportStatus.Status}");
    System.Threading.Thread.Sleep(5000); // 等待5秒再查询,避免频繁请求
} while (reportStatus.Status != ReportRequestStatusType.Completed && 
         reportStatus.Status != ReportRequestStatusType.Failed);

if (reportStatus.Status == ReportRequestStatusType.Failed)
{
    Console.WriteLine($"报告生成失败:{reportStatus.ErrorMessage}");
    return;
}

第五步:下载报告到本地

报告生成完成后,直接下载保存到本地:

// 获取报告下载链接
var downloadUrl = reportStatus.ReportDownloadUrl;

// 下载并保存到本地
using (var webClient = new System.Net.WebClient())
{
    var savePath = $"Campaign_Report_{DateTime.Now:yyyyMMddHHmmss}.csv";
    webClient.DownloadFile(downloadUrl, savePath);
    Console.WriteLine($"报告已成功下载到:{savePath}");
}

额外提醒

  • 要是你的RefreshToken过期了,得重新走授权流程获取,但你说已经有了,应该没问题
  • 可以根据需求调整Columns里的字段,比如加上ClickThroughRate(点击率)、Conversions(转化数)等,直接查CampaignPerformanceReportColumn枚举就行
  • 如果是超大报告,建议把轮询间隔拉长到10-15秒,避免触发API限流

内容的提问来源于stack exchange,提问作者John Sp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:38:40