我在导出广告统计报告时出现问题,因为我在使用ads_insights(版本2.5)时使用.NET Framework编写的代码。在我使用带有版本2.3的reportstats之前,我可以成功下载报告< / p>
我的请求是 // www.facebook.com/ads/ads_insights/export_report?report_run_id=0000000&format=xls&access_token=token
当我在浏览器中执行请求时,我可以成功下载报告(文件xls已完成),但是当我通过代码.NET Framework(C#)执行请求时,我下载了.xls不完整的文件[在此处输入图像描述] [2]
1º请求
graph.facebook.com/v2.5/act_countNumbrer/insights?level=ad&time_range=%7B%27since%27%3A%272015-11-02%27%2C%27until%27%3A%272015- 11-02%27%7D&安培; actions_group_by =%5B%27action_type%27%5D&安培;字段= CAMPAIGN_NAME%2Cad_name%2Cad_id%2Creach%2Cfrequency%2Cimpressions%2Ccpm%2Ccpp%2Cspend%2Csocial_clicks%2Cunique_clicks%2Cctr%2Cunique_ctr%2Caccount_name%2Cactions %2Ctotal_actions%2Cwebsite_clicks&安培; time_increment = 1&安培; =的access_token令牌
Result: successful -> I get a report_run_id
使用GET方法请求2º
graph.facebook.com/v2.5/id_report&access_token=token
Result: successful -> I get a
{
"id": "xxxx",
"account_id": "xxx",
"time_ref": 1447171267,
"time_completed": 1447171269,
"async_status": "Job Completed",
"async_percent_completion": 100,
}
当“async_status”为“作业完成”时,3º,我执行请求
www.facebook.com/ads/ads_insights/export_report?report_run_id=xxxx&format=xls&access_token=token
结果:我下载的文件.xls不完整。如果您在浏览器中粘贴查询(URL),则下载报告成功(文件xls已完成)enter image description here
如果我使用代码.NET Framework(C#)执行请求并将响应保存为字符串,则响应表明我们“应该更新您的浏览器”enter image description here
为什么我不能下载报告?
谢谢
用于执行下载报告的代码XLS
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace test
{
class Program
{
static void Main(string[] args)
{
string token="token";
string report_run_id="report_number";
string url = "https://www.facebook.com/ads/ads_insights/export_report?report_run_id="+report_run_id+"format=xls&access_token"+token;
//option 1
string reportDownloadUrl = "repo"+DateTime.Now.Ticks + ".xls"; ;
Stream responseStream = null;
try
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
//request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
var response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream(); //relleno el flujo
using (var fileStream = new FileStream(reportDownloadUrl, FileMode.Create, FileAccess.Write))
{
responseStream.CopyTo(fileStream);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (responseStream != null) responseStream.Close();
}
Console.WriteLine("File Download" +reportDownloadUrl);
/* //option 2
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
wc.DownloadFile(url,"repo.xls");
}
Console.WriteLine("File Download");
*/
Console.ReadKey();
}
}
}
答案 0 :(得分:0)
我遇到了同样的问题,尝试使用npm请求下载报告 的NodeJS。 添加User-Agent标头解决了我的问题。
答案 1 :(得分:0)
这对我有用。您的URL字符串中存在一些问题。 这是一个更新的url字符串:
string url = "https://www.facebook.com/ads/ads_insights/export_report?report_run_id=" + report_id + "&format=csv&access_token=" + accessToken;
此方法有效。
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
wc.DownloadFile(url, "page1.csv");
}
报告将保存到:
C:\your\path\to\project\FacebookReportPuller\bin\Debug