我正在尝试为 NetSuite 编写一个 API 连接器来连接到第三方服务,并希望将 API 密钥存储在 Secret 中。我见过几个 QA 对此表示关注,例如 this ,但我不知道如何知道 Secret 是否已成功获取。
我需要连接的服务已经过时,需要将 API 密钥作为 POST 参数发送,我想检查我的脚本是否正确从 Secret 加载密钥,但每次我尝试
log.debug()
时,什么也没有出来。下面是我尝试过的两个示例,一个使用 https
,另一个使用 crypto
。该 Secret 确实存在于我的帐户中,ID 为 custsecret_demo_key
,并且设置为可供所有脚本访问(一旦我开始工作,我将缩小范围)。其中每一个都彻底引用了 Oracle 自己的访问 Secret 的示例。
可以将 Secret 的值打印到日志中以确保其正常工作吗?如果没有,我怎么知道它是否有效?
https
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/log', 'N/https'],
function(log, https) {
const beforeLoad = (scriptContext) => {
const apiKey = https.createSecureString({input: '{custsecret_demo_key}'});
log.debug({
title: "connector test",
details: apiKey,
});
};
return {
beforeLoad: beforeLoad
};
});
日志:
{}
加密货币
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/log', 'N/crypto', 'N/encode'],
function(log, crypto, encode) {
const beforeLoad = (scriptContext) => {
const apiKey = crypto.createSecretKey({secret: '{custsecret_demo_key}', encoding: encode.Encoding.UTF_8});
log.debug({
title: "connector test",
details: apiKey,
});
};
return {
beforeLoad: beforeLoad
};
});
日志:
{"secret":"{custsecret_dt_api_key}","encoding":"UTF_8"}
前段时间我在将 Netsuite 与另一个系统集成时发现了同样的问题。然后,我将凭据保存在 FileCabinet 的 JSON 文件中,并从那里使用它们。我只是保护这样一个文件,使其只能由脚本和 Netsuite 管理员访问,以保证 ApiKey 的安全。下面的代码可以帮助您找到您的...
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/log', 'N/file'],
function(log, file) {
const beforeLoad = (scriptContext) => {
let cfgFile = file.load(
{
id: cfgFileId
}).getContents();
let cfg = JSON.parse(cfgFile);
const apiKey = cfg.custsecret_demo_key;
log.debug({
title: "connector test",
details: apiKey,
});
};
return {
beforeLoad: beforeLoad
};
});
您可以固定或动态设置
cfgFileId
。它是您在文件柜中保存凭据的文件的 ID。 JSON 文件如下所示:
{
"custsecret_demo_key": "Your key"
}