【问题标题】:Form Authentication, keep session Google Apps Script表单身份验证,保持会话 Google Apps 脚本
【发布时间】:2013-03-14 16:28:10
【问题描述】:

我有一个用于与外部程序通信的 Google Apps 脚本。该程序需要一个身份验证来进行身份验证:

"为此应使用 POST 方法,内容类型应设置为 "application/x-www-form-urlencoded"。用户名应在参数 "UserName中给出>" 并且密码应该在参数 "Password" 中给出。所以帖子数据将类似于 "UserName=xxx&Password=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;
?>

【问题讨论】:

  • 是否有理由不为第二次调用重新发送有效载荷?
  • 是的,这是对 XMLDownload 的调用以获取数据。当我想发布内容时,需要将有效负载设置为 XML 数据。当我将有效负载与调用一起添加时,没关系,但我不能同时为 auth 和 application/xml 发送 x-www-form-urlencoded?
  • 这是用 PHP 完成的:

标签: google-apps-script forms-authentication


【解决方案1】:

我认为您缺少 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 有时会改变......

【讨论】:

  • 在我看来 HttpOnly cookie 也没有返回,这可能对身份验证有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
相关资源
最近更新 更多