try 在Node JS中执行Nestuite upsert操作(https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_156335203191.html#Using-the-Upsert-Operation)时,我收到以下响应错误:-
Response:个
{"type":"https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.2","标题":"未经授权","Status":401,"o:错误详细信息":[{"Detail":"无效登录try .有关详细信息,请参阅设置&>用户/角色&>用户管理&>查看登录审核跟踪的NetSuite用户界面中的登录审核跟踪.","o:errorCode":"INVALID_LOGIN"}]}
注意:凭据是正确的,因为它在带有TBA的Postman中运行良好,并返回204响应代码.当我try 使用Node JS执行相同的操作时,我不确定出了什么问题.
Node JS code:个
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);
});
}