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执行相同的操作时,我不确定出了什么问题.

Postman screenshot:enter image description here

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);
    });
}

推荐答案

整个URL需要包含在URI编码和HMAC签名请求中.然而,您忽略了'/eid:' + externalId,它是您要访问的最终URL的一部分.用baseUrl中包含的那个试试吧.

Node.js相关问答推荐

在Android Studio中react 本机构建失败:未正确检测到Node.js版本

函数声明中的异步在没有等待的函数中做什么?

mongoose findOneAndUpdate 不更新数据库

npm 在 Windows 终端中不工作

为什么运行 yarn 命令会出错 - yargs-parser的完整性判断失败

如何解决未调用 Express 错误处理程序的问题

为什么我的 npm 脚本中的 glob 不起作用?

在mongodb中只查询整数

获取数组的至少一个元素包含子字符串的文档

MERN 堆栈项目中的 React [create-react-app] 正在提供依赖项

为什么需要在 NodeJS 应用程序中创建服务器?

yarn.lock 和 npm 的 package-lock 有什么区别?

在本地运行 Cloud Functions 会出现错误functions.config() 不可用

适用于 Windows 7 的 NodeJS

在多个 .env 文件之间切换,例如 .env.development 和 node.js

代理(如提琴手)可以与 Node.js 的 ClientRequest 一起使用吗

需要 node-gyp 的 npm install 在 Windows 上失败

nodeJS - 如何使用 express 创建和读取会话

Express.js中的bodyParser.urlencoded({extended: true }))和bodyParser.json()是什么意思?

Node.js 连接仅适用于本地主机