提问人:Frank 提问时间:4/29/2017 最后编辑:Frank 更新时间:5/4/2017 访问量:356
为什么我传递给“UrlFetchApp.fetch()”的用户凭据被拒绝?
Why are the user credentials I'm passing to `UrlFetchApp.fetch()` rejected?
问:
我有一段PHP代码,我需要将其转换为Google脚本。该代码从服务器 (URL) 提取 JSON 数据,并使用摘要式身份验证 (RFC 2617) 方法来验证用于访问数据的凭据。我没有成功地让 Google 脚本使用返回 401 错误(访问被拒绝)的 UrlFetchApp.fetch(url, opt) 行。我想这与我如何在 Google 中传递用户/密码参数有关。
作为参考,这是工作 PHP 脚本的样子:
<?php
$url = "https://myurl.com/status.php"; // API URL
$user = "ABCDE"; // user credential
$pass = "mypass"; // password credential
$fields = array(
"device" => 4,
"from" => "2010-08-22",
"to" => "2010-08-24"
) ;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch); // send the request
curl_close($ch);
print_r (json_decode($result)); // process the response
?>
以下是 Google 脚本代码:
function getStatus() {
var opt = {
serial: "ABCDE",
pass : "mypass"
};
var url = "https://myurl.com/status.php";
var response = UrlFetchApp.fetch(url, opt);
}
更新:这是我收到 401 错误的脚本:
// https://user:[email protected]/api/status.php?
//
function getstatus() {
var opt = {};
opt.headers = {"serial": "pass " + Utilities.base64Encode("ABCD" + ":" +
"123-AAA")};
var url = "https://url.com/api/status.php";
var response = UrlFetchApp.fetch(url, opt);
}
答: 暂无答案
评论
UrlFetchApp http auth