【发布时间】:2021-04-29 22:52:22
【问题描述】:
我在导出广告统计报告时遇到问题,因为代码是用 .NET Framework 编写的当我使用 ads_insights(2.5 版)时。在我使用 2.3 版的 reportstats 之前,我可以成功下载报告
我的要求是//www.facebook.com/ads/ads_insights/export_report?report_run_id=0000000&format=xls&access_token=token
当我在浏览器中执行请求时,我可以成功下载报告(文件 xls 已完成),但是当我通过代码 .NET Framework (C#) 执行请求时,我下载的文件 .xls 不完整 [在此处输入图像描述] [2]
获取报告的任务是(使用代码 .NET C#)
使用 POST 方法的 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&fields=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,
}
3º当“async_status”为“Job Completed”时,我执行请求
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();
}
}
}
【问题讨论】:
-
你是如何下载文件的?你能用你用来执行下载的代码编辑你的帖子吗?为什么不让人们在新标签页中打开该链接?
-
参考这里的文档developers.facebook.com/docs/marketing-api/insights/v2.4,您需要“链接”到指定的URL才能导出报告。提供最终用户单击的链接不是成功的解决方案,我需要应用程序自动下载文件 .xls。如果应用程序无法自动下载报告,则用于检索广告统计信息的洞察界面将无济于事 使用 2.3 版可以自动下载 .XLS 文件,现在不是不可能。现在如何自动下载 .XLS 文件?问候,
-
您是否已单步执行您的代码?我认为 Facebook 方面对此没有任何改变,当我尝试使用 PHP 或 cURL 下载我自己的报告时,它工作正常。
-
我知道这无关,但谁能告诉我在哪里可以找到异步报告的 ID?
标签: facebook