我试图通过python提取salesforce报告。我尝试过这种方法:
import requests
l = requests.get('sfinstanceurl/?un=user&pw=passw')
report = requests.get('sfinstanceurl/reportid?view=d&snip&export=1&enc=UTF-8&xf=csv',cookies=l.cookies)
print(report.content)
还有这个:
def sfdc_to_pd(reportid):
login_data = {'un': 'your_username', 'pw': 'your_password'}
with requests.session() as s:
s.get('https://your_instance.salesforce.com', params = login_data)
d = requests.get("https://your_instance.salesforce.com/{}?export=1&enc=UTF-8&xf=csv".format(reportid), headers=s.headers, cookies=s.cookies)
lines = d.content.splitlines()
reader = csv.reader(lines)
data = list(reader)
data = data[:-7]
df = pd.DataFrame(data)
df.columns = df.iloc[0]
df = df.drop(0)
return df
print df
对于两者,当我打印内容时,我得到了这个(即使状态响应总是200):
b'\r\n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n<html>\n<head>\n <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">\n\n\n\n\n\n<script>\nif (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect(\'sfinstanceurl?ec=302&startURL=%2F00O0m000000JzRc%3Fview%3Dd%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv%26snip%3D\'); } else \nif (window.location.replace){ \nwindow.location.replace(\'sfinstanceurl?ec=302&startURL=%2F00O0m000000JzRc%3Fview%3Dd%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv%26snip%3D\');\n} else {;\nwindow.location.href =\'sfinstanceurl?ec=302&startURL=%2F00O0m000000JzRc%3Fview%3Dd%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv%26snip%3D\';\n} \n</script>\n\n</head>\n\n\n</html>\n\n\n\n\n\n<!-- Body events -->\n<script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script>\n\t\t\t\n</body>\n</html>\n\n\n<!--\n...................................................................................................\n...................................................................................................\n...................................................................................................\n...................................................................................................\n-->\n'
我的公司使用SSO,所以安全令牌不是我必须尝试不同的方法。我错过了什么吗?为什么我没有在内容中获得报告?
修改
我也尝试过这种方法:
from simple_salesforce import Salesforce
import requests
import base64
import json
sf = Salesforce(username= #login
,password= # password
,security_token= # token )
print "get sid ", sf.session_id
response = requests.get("https://instancename/reportid?view=d&snip&export=1&enc=UTF-8&xf=csv",
headers = sf.headers, cookies = {'sid' : sf.session_id})
response.content
它仍然返回相同的HTML对象。
答案 0 :(得分:3)
看起来您正在直接从SF实例运行报告,这将始终返回HTML,因为它不是API调用。
要通过API访问Reports,请参阅Salesforce Reports和Dashboards REST API的文档:
以下是专门针对报告的资源的链接:
使用Simple Salesforce时,您可以轻松调用API,如下所示:
from simple_salesforce import Salesforce
import requests
import base64
import json
sf = Salesforce(username=[username],password=[password],security_token=[token])
reportId = '00OR0000000K2UeMAK'
reportRESTPath = ('analytics/reports/{id}'.format(id='00OR0000000K2UeMAK')
# Synchronous report get
reportJSON = sf.restful(reportRESTPath, {'includeDetails': 'true'})
# Asynchronous report create
asyncJobJSON = sf.restful(reportRESTPath, {'includeDetails': 'true'}, 'POST')
# Asynchronous report get
reportJSON = sf.restful(('{basePath}/instances/{instanceId}'.format(basePath=reportRESTPath,instanceId=asyncJobJSON.Id), {'includeDetails': 'true'})