【问题标题】:Google Apps Script - Making a url request using sha1 and base64 encodeGoogle Apps 脚本 - 使用 sha1 和 base64 编码发出 url 请求
【发布时间】:2017-12-04 08:32:35
【问题描述】:

我必须使用 Google Apps 脚本发出 url 请求,为此,我需要使用 sha 1 算法和 base64 编码对我的登录名和密码进行编码。在请求的文档中,我有以下 php 代码:

$string = "loginpassword2017-15-04T09:25:00"; $hash = sha1($string, true); $hash = base64_encode($hash);

所以在 Google Apps 脚本中,我使用以下代码:

function GetSHA1(input) { var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input); var txtHash = ''; for (j = 0; j <rawHash.length; j++) { var hashVal = rawHash[j]; if (hashVal < 0) hashVal += 256; if (hashVal.toString(16).length == 1) txtHash += "0"; txtHash += hashVal.toString(16); } return txtHash; }

然后是函数

Utilities.base64Encode

最后我发出了 url 请求,但身份验证不成功。有人知道如何在 Google Apps Script 中执行 php sha1 和 base64encode 吗?谢谢

【问题讨论】:

  • “身份验证不成功”是什么意思?
  • 这是一个连接到 web 服务的 url 请求我实际上发现了问题,我在谷歌应用程序脚本上测试的不同 sha1 与 php 不同(我在 php 中测试了加密并在谷歌应用程序脚本,它工作)
  • 现在我的问题是找到一个与 php 相同的 google apps 脚本

标签: google-apps-script


【解决方案1】:

您可以将 Utilities::computeDigest 返回的 Byte[] 数组直接传递给 Utilities::base64EncodeWebSafe 调用。因此,您可以执行以下操作:

function getLoginHash(loginData) {

    return Utilities.base64EncodeWebSafe(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, loginData));

}

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 2021-11-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多