javascript - Hmac.digest 返回 AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID
问题描述
我正在尝试创建一个使用套件脚本 2.0 生成 OAuth 签名的函数。但是,我遇到“AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID - 解密凭据时出错”。使用 hmac.digest 函数时出错。有什么想法我哪里出错了吗?
var generateOAuthSignature = function(params) {
require(['N/crypto', 'N/encode'], function(crypto, encode) {
var timestamp = Math.round((new Date()).getTime() / 1000.0);
var nonce = encode.convert({
string: timestamp + "",
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.BASE_64
});
var oauth_headers = {
oauth_version: "1.0",
oauth_nonce: nonce,
oauth_signature_method: params.signature_method,
oauth_consumer_key: params.consumer_key,
oauth_token: params.token,
oauth_timestamp: timestamp
};
log.debug('oauth_headers', oauth_headers)
var url_params = params.rest_url.split("?")[1].split("&");
var signature_params = {};
for (key in oauth_headers) {
signature_params[key] = key + "=" + oauth_headers[key];
}
for (key in url_params) {
var temp = url_params[key].split("=");
signature_params[temp[0]] = url_params[key];
}
var signature_string = "";
var sortedkeys = Object.keys(signature_params).sort();
for (var i = 0; i < sortedkeys.length; i++) {
signature_string += (i == 0 ? "" : "&") + signature_params[sortedkeys[i]];
}
var base_string = params.method + "&" + encodeURIComponent(params.rest_url.split("?")[0]) + "&" + encodeURIComponent(signature_string);
var composite_key_guid = encodeURIComponent(params.consumer_secret) + "&" + encodeURIComponent(params.token_secret);
log.debug('composite_key_guid', composite_key_guid);
log.debug("crypto.createSecretKey({guid: composite_key, encoding: encode.Encoding.UTF_8})", JSON.stringify(crypto.createSecretKey({ guid: composite_key_guid, encoding: encode.Encoding.UTF_8 })));
var secret_key = crypto.createSecretKey({
guid: composite_key_guid, encoding: encode.Encoding.UTF_8
});
log.debug('secret_key', secret_key);
var hmac = crypto.createHmac({
algorithm: crypto.HashAlg.SHA256,
key: secret_key
});
hmac.update({
input: base_string,
inputEncoding: encode.Encoding.UTF_8
});
// error occuring here
oauth_signature = hmac.digest({
outputEncoding: encode.Encoding.BASE_64
});
//more code below...
});
}
解决方案
问题是guid
. NetSuiteguid
不是您编造的东西,它是“对表单凭据字段中的密码值的引用”。
资源:https://{yourAppId}.app.netsuite.com/app/help/helpcenter.nl?fid=section_4358549582.html
制作凭证字段是一种痛苦。即使在 XML 中定义 PASSWORD 字段类型也不会这样做。
/**
* @NApiVersion 2.x
*/
require(['N/crypto', 'N/encode', 'N/runtime'], function(crypto, encode, runtime) {
function createSecureKeyWithHash() {
var inputString = 'YWJjZGVmZwo=';
// the guid MUST reference a credential field in NetSuite
var myGuid = '{284CFB2D225B1D76FB94D150207E49DF}';
// if the guid is invalid, this doesn't really return anything
var sKey = crypto.createSecretKey({
guid: myGuid,
encoding: encode.Encoding.UTF_8
});
// similarly, since the secret key is invalid, this silently returns nothing
var hmacSHA512 = crypto.createHmac({
algorithm: crypto.HashAlg.SHA512,
key: sKey
});
hmacSHA512.update({
input: inputString,
inputEncoding: encode.Encoding.BASE_64
});
// by the time you get here, all the methods above will have run, but they
// haven't actually returned anything, because the guid was bad. This is
// first time the code throws and error
var digestSHA512 = hmacSHA512.digest({
outputEncoding: encode.Encoding.HEX
});
}
createSecureKeyWithHash();
});
推荐阅读
- c# - 获取发送文件请求时远程主机强制关闭现有连接
- r - rowSums(out_pathway) 中的错误:“x”必须是至少二维的数组
- javascript - Typo 不想在 ios 和 android 项目上链接
- actionscript-3 - AS3:拖放对象的触摸事件问题
- css - JavaFX - 不同的默认 CSS 窗口和 linux?
- laravel - laravel 验证不适用于 Auth
- paraview - paraview:加载共享库时出错:libQt5Core.so.5:无法打开共享对象文件:没有这样的文件或目录
- asp.net - 具有不变名称“Oracle.DataAccess.Client”的 ADO.NET 提供程序未注册或无法加载
- php - 了解查询并将其转换为 Oracle SQL
- c# - 使用 Xelement 时如何换行