403 错误

问题描述 投票:0回答:1

我尝试通过从 Node.js 调用已部署的 NetSuite Restlet 脚本在 NetSuite 中创建客户记录。我收到 403 错误响应。我可以通过Postman成功调用脚本。我不确定从 Node.js 调用时出现什么问题,因为代码中的授权部分看起来是正确的。这是 Node.js 中的代码:

function upsertUsingRestlet(body, callbackFn) {

  const baseUrl = `https://${NETSUITE_ACCOUNT_ID}.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=${SCRIPT_ID}&deploy=${DEPLOYMENT_ID}`;
  const oauthSignatureMethod = 'HMAC-SHA256';
  const oauthVersion = '1.0';
  const oauthNonce = crypto.randomBytes(32).toString('hex');
  const oauthTimestamp = Math.floor(Date.now() / 1000);

  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 = `POST&${encodeURIComponent(baseUrl)}&${encodeURIComponent(sortedParameters)}`;
  const signingKey = `${CONSUMER_SECRET}&${ACCESS_TOKEN_SECRET}`;
  const hmac = crypto.createHmac('sha256', signingKey);
  hmac.update(signatureBaseString);
  let oauthSignature = hmac.digest('base64');
  oauthSignature = encodeURIComponent(oauthSignature);

  const headers = {
    'Prefer': 'transient',
    'Content-Type': 'application/json',
    'Authorization': `OAuth realm="${REALM}",oauth_nonce="${oauthNonce}",oauth_signature_method="${oauthSignatureMethod}",oauth_consumer_key="${CONSUMER_KEY}",oauth_token="${ACCESS_TOKEN}",oauth_timestamp="${oauthTimestamp}",oauth_version="${oauthVersion}",oauth_signature="${oauthSignature}"`
  };

  fetch(baseUrl, {
    'method': 'POST',
    'headers': headers,
    'body': body,
    'redirect': 'follow'
  })
    .then((response) => response)
    .then((data) => {
      callbackFn(data.status);
    })
    .catch((error) => {
      console.error('Error upsertOperation:', error);
    });
}

在 Netsuite 中,查看登录和审核跟踪 - 我在详细信息列中看到错误为 InvalidSignature。

但是,当我使用上述代码调用 netsuite REST API 时,相同的授权可以成功工作。唯一的区别是,这是一个 PUT 请求,而不是 POST。所以,我知道为什么它不能与 Restlet 一起使用。

Restlet 脚本部署截图供参考:-

node.js netsuite suitescript suitescript2.0 netsuite-rest-api
1个回答
-2
投票

403 错误通常表示服务器理解该请求但拒绝授权。您可以采取以下几个步骤来解决从 Node.js 调用 NetSuite Restlet 时出现的 403 错误:

检查身份验证凭据:确保 Node.js 应用程序中用于访问 NetSuite Restlet 的身份验证凭据(令牌、密钥、用户名、密码)正确,并且具有访问资源所需的权限。

验证端点和标头:仔细检查您用于访问 Restlet 的 URL 端点以及随请求发送的标头。 NetSuite 可能对需要包含的标头(例如身份验证令牌或内容类型)有特定要求。

跨源资源共享(CORS):如果您从客户端应用程序(例如浏览器)发出请求,服务器可能会由于 CORS 限制而阻止该请求。确保 NetSuite Restlet 允许来自托管 Node.js 应用程序的域的请求。您可能需要在 Restlet 代码或 NetSuite 帐户中配置 CORS 设置。

IP 白名单或防火墙:NetSuite 可能具有 IP 白名单或防火墙规则,限制对某些 IP 地址或范围的访问。如果 NetSuite 阻止来自未经授权的 IP 的请求,请确保您的 Node.js 服务器的 IP 地址已列入白名单。

检查速率限制:有时,短时间内过多的请求可能会触发服务器端的速率限制。检查您是否太快地向 NetSuite Restlet 发送了太多请求。

日志记录和错误处理:在 Node.js 应用程序中实现强大的日志记录和错误处理,以捕获更具体的错误详细信息。这可以深入了解 403 错误的确切原因。

联系 NetSuite 支持:如果上述步骤均无法解决问题,则联系 NetSuite 的支持或文档可能会提供具体指导或对错误的深入了解。

请记住在 Node.js 代码中优雅地处理错误,以便为用户提供更好的反馈或用于调试目的。

© www.soinside.com 2019 - 2024. All rights reserved.