2016-11-29 74 views
0

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

请看看我的代码:

#!/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: [email protected]" if [email protected]; 

# 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格式,而不是荒诞。我尝试使用工作台并获得JSON格式的响应,这很好。反应不正确。

+2

欢迎来到Stack Overflow。您的帖子中突出显示的语法有点不对劲。请[编辑]问题,并看看。 – simbabque

+2

它看起来像你需要登录。该页面似乎重定向到_login.salesforce.com_。也许在你的代码开始处'eval'没有做你的想法。你看过'sforce'的内容吗? – simbabque

回答

0

为了让您的报告的JSON尝试以下操作:

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

这为我工作。

+0

https:// .salesforce.com/services/data/v35.0/analytics/reports/