Node.js中TBA调用NetSuite REST Upsert报401 INVALID_LOGIN错误
NetSuite Upsert操作Node.js实现报401无效登录问题
我在Node.js中执行NetSuite的Upsert操作时,收到401 Unauthorized错误,错误响应如下:
响应内容:
{"type":"https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.2","title":"Unauthorized","status":401,"o:errorDetails":[{"detail":"Invalid login attempt. For more details, see the Login Audit Trail in the NetSuite UI at Setup > Users/Roles > User Management > View Login Audit Trail.","o:errorCode":"INVALID_LOGIN"}]}
备注:凭证确认正确,因为使用TBA在Postman中执行相同操作可正常返回204状态码,不清楚Node.js实现中哪里出错。
Postman截图:
Node.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}` // 备注:已将NetSuite账户ID中的空格替换为下划线,小写字母转为大写,此处未提供实际值 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); }); }
内容的提问来源于stack exchange,提问作者user19976880
相关产品推荐
相关产品推荐

