将 Salesforce 报告导出为 CSV [英] Export salesforce report as CSV

查看:68
本文介绍了将 Salesforce 报告导出为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Python 提取 Salesforce 报告.我试过这种方法:

I'm trying to pull salesforce reports through python. I've tried this approach:

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):

And for both, when i print the content I get this (even though the status response is always 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,因此安全令牌不是我必须尝试不同方法的东西.我错过了什么吗?为什么我在内容中没有得到报告?

My company uses SSO, so security tokens are not something i have to try a different approach. Am I missing something? Why do I not get the report in the content?

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

I also just tried this approach:

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

It still returns the same HTML object.

推荐答案

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

Looks like you are running reports from the SF instance directly, this will always return HTML as it is not an API call.

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

To access Reports through an API please refer to the documentation for Salesforce Reports and Dashboards REST API:

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

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

Below is a link to the resources specifically for reports:

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

使用 Simple Salesforce 时,您可以轻松调用 API,如下所示:

When using Simple Salesforce you can easily call the API, like this:

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

这篇关于将 Salesforce 报告导出为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆