Perl REST与Salesforce Report的集成

时间:2016-11-29 10:25:24

标签: perl salesforce

我正在将Perl与Salesforce Reports集成。我正在尝试通过Salesforce Report的标准REST API(/ 00O93000009NpOy?export = 1& enc = UTF-8& xf = csv)连接Perl脚本并获取响应代码200.在高级别我需要在Excel中下载Salesforce报告使用Perl脚本格式化。

请查看我的代码:

#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;
use WWW::Salesforce;
use REST::Client;

# Authenticate first via SOAP interface to get a session ID:
my $sforce = eval { WWW::Salesforce->login(
                    'username' => "USER_NAME",
                    'password' => "PASSWORD" ); };
die "Could not login to SFDC: $@" if $@;

# Get the session ID:
my $hdr = $sforce->get_session_header();
my $sid = ${$hdr->{_value}->[0]}->{_value}->[0];


my $host = 'https://ap1.salesforce.com';
my $client = REST::Client->new(host => $host);

 # Get ALL incidents
 $client->GET('/00O93000009NpOy?export=1&enc=UTF-8&xf=csv',
              {'Authorization' => "OAuth $sid",
               'Accept' => 'application/json'});

 print 'Response: ' . $client->responseContent() . "\n";
 print 'Response status: ' . $client->responseCode() . "\n";
 foreach ( $client->responseHeaders() ) {
   print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
 }

当我通过命令提示符运行此Perl代码时,我得到了这样的响应:

C:\Users\Documents>perl TEST.pl`enter code here`
Response:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
g/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">





<script>
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNaviga
tor.handleRedirect('https://login.salesforce.com/?ec=302&startURL=%2F00O90000009
NhOy%3Fenc%3DUTF-8%26export%3D1%26xf%3Dcsv'); }  else
if (window.location.replace){
window.location.replace('https://login.salesforce.com/?ec=302&startURL=%2F00O900
00009NhOy%3Fenc%3DUTF-8%26export%3D1%26xf%3Dcsv');
} else {;
window.location.href ='https://login.salesforce.com/?ec=302&startURL=%2F00O90000
009NhOy%3Fenc%3DUTF-8%26export%3D1%26xf%3Dcsv';
}
</script>

</head>


</html>





<!-- Body events -->
<script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){w
indow.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload()
{}function bodyOnFocus(){}function bodyOnUnload(){}</script>

</body>
</html>


<!--
................................................................................
...................
................................................................................
...................
................................................................................
...................
................................................................................
...................
-->

Response status: 200
Header: Connection=close
Header: Date=Tue, 29 Nov 2016 10:05:43 GMT
Header: Pragma=NO-CACHE
Header: Content-Type=text/html;charset=UTF-8
Header: Client-Date=Tue, 29 Nov 2016 10:05:43 GMT
Header: Client-Peer=182.50.78.41:443
Header: Client-Response-Num=1
Header: Client-SSL-Cert-Issuer=/C=US/O=Symantec Corporation/OU=Symantec Trust Ne
twork/CN=Symantec Class 3 Secure Server CA - G4
Header: Client-SSL-Cert-Subject=/C=US/ST=California/L=San Francisco/O=Salesforce
.com, Inc/OU=Applications/CN=*.salesforce.com
Header: Client-SSL-Cipher=ECDHE-RSA-AES256-GCM-SHA384
Header: Client-SSL-Socket-Class=IO::Socket::SSL
Header: Set-Cookie=BrowserId=UGPsAKnYTgWN3JcU4kcKxg;Path=/;Domain=.salesforce.co
m;Expires=Sat, 28-Jan-2017 10:05:43 GMT

虽然响应应该是JSON格式而不是荒谬的。我尝试使用workbench并以JSON格式获得响应,这很好。反应不恰当。

1 个答案:

答案 0 :(得分:0)

要获取报告的json,请尝试以下操作:

$client->GET('https://<server name>.salesforce.com/services/data/v35.0/analytics/reports/<report id>',
              {'Authorization' => "OAuth $sid",
               'Accept' => 'application/json'});

这对我有用。