2013-03-14 53 views
0

我有一個Google Apps腳本與外部程序進行通信。該程序需要驗證身份驗證:表單身份驗證,保留會話Google Apps腳本

「POST方法應該用於此,內容類型應該設置爲」application/x-www-form-urlencoded「。用戶名應該在參數「用戶名」,密碼應在參數「密碼」中給出。因此,POST數據將會像「用戶名 = XXX & 密碼 = YYY」。請記住,當不發送用戶名和密碼的每個請求,會話(或保持它的對象)應該保持活着。「

這是工作:

var payload = "_UserName_="+username+"&_Password_="+password 
var options = { 
"method" : "POST", 
"payload" : payload, 
"contentType" : "application/x-www-form-urlencoded" 
}; 

var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDivisions.aspx",  options); 

但在這裏我想作下一個請求:

var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDownload.aspx?PartnerKey="+partnerKey+"&Topic=Accounts"); 

但沒有通過認證?

我該如何讓GAS會話保持活躍狀態​​?

工作示例使用PHP:

<?php 
$baseurl = "https://start.exactonline.nl"; 
$username = "<username>"; 
$password = "<password>"; 
$partnerkey = ""; /* If you have one, the partnerkey with or without curly braces */ 
$division = "<division code>"; /* Check the result of the first division retrieving for the division codes */ 
$cookiefile = "cookie.txt"; 
$crtbundlefile = "cacert.pem"; /* this can be downloaded from http://curl.haxx.se/docs/caextract.html */ 


/* Logging in */ 
$header[1] = "Cache-Control: private"; 
$header[2] = "Connection: Keep-Alive"; 

/* init, don't term until you're completely done with this session */ 
$ch = curl_init(); 

/* Set all options */ 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
curl_setopt($ch, CURLOPT_CAINFO, $crtbundlefile); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array("_UserName_"=>"$username", "_Password_"=>"$password")); 

/* Retrieving divisions, the current should be the one in which the user worked the last time */ 
$url = "$baseurl/docs/XMLDivisions.aspx"; 
curl_setopt($ch, CURLOPT_URL, $url); 
$result = curl_exec($ch); 

/* Switching divisions */ 
$url = "$baseurl/docs/ClearSession.aspx?Division=$division&Remember=3"; 
curl_setopt($ch, CURLOPT_URL, $url); 
$result = curl_exec($ch); 

/* Retrieving divisions, the current should now be the one supplied in the switching divisions. This is only the current for this session! */ 
$url = "$baseurl/docs/XMLDivisions.aspx"; 
curl_setopt($ch, CURLOPT_URL, $url); 
$result = curl_exec($ch); 

/* Upload an xml file */ 
$topic = "Invoices"; 
$url = "$baseurl/docs/XMLUpload.aspx?Topic=$topic&PartnerKey=$partnerkey"; 
curl_setopt($ch, CURLOPT_URL, $url); 
/* Send the xml along with the request */ 
$filename = "Invoices.xml"; 
$fp = fopen($filename, "r"); 
$xml = fread($fp, filesize($filename)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, utf8_encode($xml)); 
$result = curl_exec($ch); 

/* Finally close as we're finished with this session */ 
curl_close($ch); 

echo $result; 
?> 
+0

是否有任何理由不重新發送第二個呼叫的有效載荷? – Jonathon 2013-03-14 17:06:12

+0

是的,這是一個調用XMLDownload獲取數據。 當我想發佈內容時,需要將有效負載設置爲XML數據。 當我將有效負載與調用一起添加時,它是好的,但是我不能同時發送一個x-www-form-urlencoded用於auth和application/xml。 – 2013-03-14 19:32:27

+0

這是如何使用PHP完成的: – 2013-03-14 19:47:57

回答

3

我認爲你缺少的cookie。

嘗試這樣:

var payload = "_UserName_="+username+"&_Password_="+password 
var options = { 
"method" : "POST", 
"payload" : payload, 
"contentType" : "application/x-www-form-urlencoded" 
}; 

var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDivisions.aspx",  options); 

var cookie = response.getAllHeaders()['Set-Cookie']; 

然後,沿着你的其他準備頭髮送cookie中的第二個請求

var header = {'Cookie':cookie}; 
var opt = {"headers":header}; 

var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDownload.aspx?PartnerKey="+partnerKey+"&Topic=Accounts",opts); 

我認爲應該工作,但有時服務器在cookie不是隻需要會話,而且相同的IP地址應請求,在這種情況下,谷歌IP的有時會改變......