我正在使用Bing Ads API从bing上运行的在线广告中获取广告系列统计信息(点击次数,展示次数等)。
要检索这些统计信息,您需要通过报告服务发出2个API请求。
我遇到的问题是,有时,SubmitGenerateReport请求需要额外的一两秒才能返回此ReportRequestId,因此页面遇到错误“Invalid ReportRequestId” - 因为它还没有。当我重新加载页面时,它会解决大部分时间(如果没有,则会额外重新加载或两次重新加载)。
我尝试在第一次请求后添加一个Sleep命令(正如您将在下面看到的那样),但它似乎无法解决问题。
有没有更好的选择来解决这个问题? Bing API团队没有太多帮助。
adwordscampaign_controller.rb
#Start Bing Reporting Code - acquire report request ID number
client = Savon.client("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl")
@response = client.request :v8, :submit_generate_report, xmlns: "https://adcenter.microsoft.com/v8" do
soap.namespaces["xmlns:v8"] = "https://adcenter.microsoft.com/v8"
soap.namespaces["xmlns:i"] = "http://www.w3.org/2001/XMLSchema-instance"
soap.namespaces["xmlns:a1"] ="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
soap.header = "<v8:UserName>#{binguser}</v8:UserName><v8:Password>#{bingpass}</v8:Password><v8:DeveloperToken>[removed]</v8:DeveloperToken>"
soap.body = "<ReportRequest i:nil=\"false\" i:type=\"CampaignPerformanceReportRequest\"><Format i:nil=\"false\">Xml</Format><Language i:nil=\"false\">English</Language>[shortened for the sake of reader sanity]</ReportRequest>"
end
#adding delay to compensate for slow turnaround by report generation
sleep 1.5
@responsehash = @response.to_hash
@responsehashdeep = @responsehash[:submit_generate_report_response][:report_request_id]
@report_req_id = @responsehashdeep.to_s
reportreq = @report_req_id
#report request ID acquired
#acquire Bing report download URL
reportclient = Savon.client("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl")
@reportresponse = reportclient.request :v8, :poll_generate_report do
soap.namespaces["xmlns:v8"] = "https://adcenter.microsoft.com/v8"
soap.header = "<v8:UserName>[removed]</v8:UserName><v8:Password>[removed]</v8:Password><v8:DeveloperToken>[removed]</v8:DeveloperToken><v8:CustomerId>[removed]</v8:CustomerId><v8:CustomerAccountId>[removed]</v8:CustomerAccountId>"
soap.body = "<v8:ReportRequestId>#{reportreq}</v8:ReportRequestId>"
end
@reportresponsehash = @reportresponse.to_hash
@reportresponsehashdeep = @reportresponsehash[:poll_generate_report_response][:report_request_status][:report_download_url]
reporturl = @reportresponsehashdeep.to_s
#report download URL acquired
答案 0 :(得分:1)
如果Bing没有用于检查报告状态的API,则可以捕获异常并重试。在后一种情况https://github.com/nfedyashev/retryable
中,这可能会有所帮助考虑到你在睡觉的地方,我想你也可能会对发生的事情感到困惑。当请求返回时,响应始终存在,但是当您进行第二次API调用时,报告尚未就绪。