在Salesforce stackexchange上询问此问题,在此发布更广泛的受众群体。 我的目标是导出Salesforce.com报告。从浏览器它可以正常工作,如果我已登录并将以下URL粘贴到我的浏览器的地址栏,文件将被下载: “https://na9.salesforce.com/00OE0000001L8cUMAS/?export=1&enc=UTF-8&xf=CSV”
但是,我需要从我的.NET应用程序中导出一堆报告。因此,使用Analitics API,我可以获得我需要的报告ID。然后我建立相同的网址:
"https://na9.salesforce.com/00OE0000001L8cUMAS/?export=1&enc=UTF-8&xf=CSV"
然后我尝试执行以下操作:
WebClient wc = new WebClient();
wc.Headers["Authorization"] = "Bearer " + sForceService.SessionHeaderValue.sessionId;
wc.DownloadFile(URL, Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "report.csv"));
但这不会保存报告,而是保存以下html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<script>
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8'); } else
if (window.location.replace){
window.location.replace('https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8');
} else {;
window.location.href ='https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8';
}
</script>
</head>
</html>
<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->
看起来像是一些重定向javascript,所以我尝试做WebClient.DownloadFile,传递这个重定向url:
string URL = "https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8";
WebClient wc = new WebClient();
wc.Headers["Authorization"] = "Bearer " + sForceService.SessionHeaderValue.sessionId;
wc.DownloadFile(URL, Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "report.csv"));
但这只是返回标准的salesforce登录屏幕,说我需要登录。(尽管我总是在标题中传递一个活动的sessionId)。 我不相信没有办法以编程方式导出报告,如果我可以在浏览器中执行,我应该能够从代码中执行此操作,我只是缺少一些基本的东西...... sessionId应该以不同的方式传递吗?