在尝试执行 Nestuite upsert 操作时,我收到以下错误响应(https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_156335203191.html#Using-the -Upsert-Operation) 在 Node JS 中:-
回复:
{“类型”:“https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.2”,“标题”:“未经授权”,“状态”:401,“o:错误详细信息":[{"detail":"登录尝试无效。有关详细信息,请参阅 NetSuite UI 中的登录审核跟踪(位于设置 > 用户/角色 > 用户管理 > 查看登录审核跟踪)。","o:errorCode":" INVALID_LOGIN"}]}
注意:凭据是正确的,因为它在带有 TBA 的 Postman 中完美运行并返回 204 响应代码。当我尝试使用 Node JS 做同样的事情时,不知道出了什么问题。
节点JS代码:
const fetch = require('node-fetch');
const crypto = require('crypto');
function upsertOperation(externalId, body) {
const baseUrl = `https://${NETSUITE_ACCOUNT_ID}.suitetalk.api.netsuite.com/services/rest/record/v1/${RESOURCE}`;
const oauthNonce = crypto.randomBytes(32).toString('hex');
const oauthTimestamp = Math.floor(Date.now() / 1000);
const oauthSignatureMethod = 'HMAC-SHA256';
const oauthVersion = '1.0';
const realm = `${REALM}` // Note: Replaced space with underscore and lower case letters with upper case letters in netsuite account id. Not provided exatct value here.
const oauthParameters = {
oauth_consumer_key: CONSUMER_KEY,
oauth_token: ACCESS_TOKEN,
oauth_nonce: oauthNonce,
oauth_timestamp: oauthTimestamp,
oauth_signature_method: oauthSignatureMethod,
oauth_version: '1.0'
};
const sortedParameters = Object.keys(oauthParameters)
.sort()
.map((key) => `${key}=${oauthParameters[key]}`)
.join('&');
const signatureBaseString = `PUT&${encodeURIComponent(baseUrl)}&${encodeURIComponent(sortedParameters)}`;
const signingKey = `${CONSUMER_SECRET}&${ACCESS_TOKEN_SECRET}`;
const hmac = crypto.createHmac('sha256', signingKey);
hmac.update(signatureBaseString);
const oauthSignature = hmac.digest('base64');
const headers = {
'Prefer': 'transient',
'Content-Type': 'application/json',
'Authorization': `OAuth realm="${realm}",oauth_signature="${oauthSignature}",oauth_nonce="${oauthNonce}",oauth_signature_method="${oauthSignatureMethod}",oauth_consumer_key="${CONSUMER_KEY}",oauth_token="${ACCESS_TOKEN}",oauth_timestamp="${oauthTimestamp}",oauth_version="${oauthVersion}"`
};
fetch(baseUrl+'/eid:' + externalId, {
method: 'PUT',
headers: headers,
body: JSON.stringify(body),
redirect: 'follow'
})
.then((response) => response.json())
.then((data) => {
console.log('data: ' + JSON.stringify(data));
})
.catch((error) => {
console.error('Error upsertOperation:', error);
});
}
整个 URL 需要包含在 URI 编码和 HMAC 签名请求中。但是,您遗漏了
'/eid:' + externalId
,它构成您要访问的最终 URL 的一部分。尝试使用 baseUrl
中包含的内容。