Salesforce: Download Reports via URL in R

泄露秘密 提交于 2020-03-26 05:45:07

问题


I try to download the reports available in Salesforce via the URL, e.g.

http://YOURInstance.my.salesforce.com/012389u13541?export=1&enc=UTF-8&xf=csv

in R.

I already did some investigation to access the report via HTTR-GET, however, up until today without any meaningful outcomes. Unfortunately, R is downloading HTML-code instead of the desired csv file. I also tried to realize the approach suggested here:

https://salesforce.stackexchange.com/questions/47414/download-a-report-using-python

The package "RForcecom" allows the interaction via an API, but I was not able to figure out how to realize above solution in R.

General GET-Request:

GET("http://YOUR_Instance.my.salesforce.com/012389u13541?export=1&enc=UTF-8&xf=csv")

I expect the output to be in csv format, but I receive the report data as html source code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3...
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
...

Did anyone of you guys encounter same issues and can provide guidance? Any kind of help is much appreciated. Thanks in advance!

UPDATED and not-working R-Snippet:

library(RForcecom)
library(httr)
username='username'
password='password'
instanceURL <- "https://login.salesforce.com/"
session <- rforcecom.login(username, password, instanceURL)
sid=as.character(session['sessionID'])

url='http://YOURInstance.my.salesforce.com/012389u13541?export=1&enc=UTF-8&xf=csv'
getData=GET(url,add_headers('Content-Type'='application/json','Authorization'=paste0("Bearer ",sid),'X-PrettyPrint'='1'),set_cookies('sid'=sid))

回答1:


Are you sure you have a valid report id? It doesn't look right (did you just obfuscate it for purposes of this post?). What is in that HTML you're getting, an error message? SF login screen?

What you're doing is effectively "screen scraping". This is not a real API, it can break at any time, you should find/build something that properly uses Salesforce Analytics API. You've been warned.

But if you're after a quick and dirty solution...

You need to pretend you're an authenticated user, that you have a valid session id. Add a cookie to your GET request.

How to get a valid session id?

  • You'd have to log in to SF first (for example use SOAP API's login call or I listed some REST api ideas here: https://stackoverflow.com/a/56034159/313628 )
  • or display some user's session ID in a SF formula, visualforce page and user would copy-paste it to your app.

Once you have it - add a Cookie header to your GET with value sid=<session id goes here>

Here's a raw request & response in SoapUI.




回答2:


In your example, I don't think that you can use the rforcecom session with httr functions as you are trying.

Here is a slightly different way to solve the problem.

Rather than trying to retrieve a report that you already created in Salesforce, why not specify the report in SOQL and use rforcecom.query function to execute the SOQL from r. That would return the data in a data frame and would require no further data wrangling in r to make it useable.

I use this technique often and once you get used to the Salesforce API I think that its probably faster and more powerful for most use cases.

Here is a simple function that I use to return select opportunity data for all opportunities in Salesforce.

getSFOpps <- function(session) {

    #Construct SOQL Query
    soql <- "SELECT Id,
                Name,
                AccountId,
                Amount,
                CurrencyIsoCode,
                convertCurrency(Amount) usd_amount,
                CloseDate,
                CreatedDate,
                Region__c,
                IsClosed,
                IsWon,
                LastActivityDate,
                LeadSource,
                OwnerId,
                Probability,
                StageName,
                Type,
                IsDeleted
                    FROM Opportunity"

    #Retrieve Opp information
        as_tibble(RForcecom::rforcecom.query(session, soql))
}

It requires that you pass in a valid session from Rforcecom.login but you seem to have that part working from your code above.

I hope this helps ...




回答3:


I recently struggled with the same issue, there's a magic parameter you need to add to the query : isdtp=p1

so if you try: http://YOURInstance.my.salesforce.com/012389u13541?export=1&enc=UTF-8&xf=csv&isdtp=p1

it should return you the file directly.



来源:https://stackoverflow.com/questions/56135216/salesforce-download-reports-via-url-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!