表单身份验证,保持会话Google Apps脚本 [英] Form Authentication, keep session Google Apps Script
本文介绍了表单身份验证,保持会话Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Google Apps脚本来与外部程序通信。程序需要身份验证才能进行身份验证:
"为此应使用POST方法,内容类型应设置为""UserName=xxx&;Password=yyy"./x-www-form-urlencode"。用户名应在参数"用户名"中给出,密码应在参数"密码"中给出。因此POST数据将类似于应用程序。请记住,当不发送每个请求的用户名和密码时,应保持会话(或保持会话的对象)处于活动状态。"
这起作用了:
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;
?>
推荐答案
我认为您缺少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有时会改变…
这篇关于表单身份验证,保持会话Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文