gpt4 book ai didi

python - 将 Salesforce 报告导出为 CSV

转载 作者:行者123 更新时间:2023-11-30 22:22:04 24 4
gpt4 key购买 nike

我正在尝试通过 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,因此安全 token 不是我必须尝试不同方法的东西。我错过了什么吗?为什么我在内容中看不到报告?

编辑:

我也刚刚尝试过这种方法:

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 对象。

最佳答案

看起来您正在直接从 SF 实例运行报告,这将始终返回 HTML,因为它不是 API 调用。

要通过 API 访问报表,请参阅 Salesforce 报表和仪表板 REST API 的文档:

https://developer.salesforce.com/docs/atlas.en-us.api_analytics.meta/api_analytics/sforce_analytics_rest_api_intro.htm

以下是专门用于报告的资源的链接:

https://developer.salesforce.com/docs/atlas.en-us.api_analytics.meta/api_analytics/sforce_analytics_rest_api_get_reportdata.htm

使用 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'})

关于python - 将 Salesforce 报告导出为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com