在 NEST.JS 中通过“q”参数搜索时,netsuite REST 出现 401 错误,适用于 postman OAuth1.0

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

我正在使用 NetSuite API,并尝试按照文档 aqui 使用查询参数(特别是“q”参数)检索员工。我正在使用以下端点: 获取 https://demo123.suitetalk.api.netsuite.com/services/rest/record/v1/customer?q=email START_WITH 芭芭拉。 当我在 Postman 中发出指向端点的请求时,一切正常,如屏幕截图所示shown in this screenshot postman。但是,当我尝试在代码中使用“q”参数发出请求时,出现以下错误:“o:errorCode”:“INVALID_LOGIN”。

这是我的代码

   async conectNetSuiteTests(): Promise<any> {
     

     const baseUrl = `${NETSUITEURL}}/services/rest/record/v1/employee?q=custentity_bit_lt_entitydocument+CONTAIN+${documentEmployee}'`;

      const oauthNonce = crypto.randomBytes(5).toString('hex');
      const oauthTimestamp = Math.floor(Date.now() / 1000);
      const oauthSignatureMethod = 'HMAC-SHA256';
      const oauthVersion = '1.0';
      const realm = `5900181_SB1`;

      const oauthParameters = {
          oauth_consumer_key: process.env.CONSUMER_KEY,
          oauth_token: process.env.ACCESS_TOKEN,
          oauth_nonce: oauthNonce,
          oauth_timestamp: oauthTimestamp,
          oauth_signature_method: oauthSignatureMethod,
          oauth_version: oauthVersion,
      };


      //   // Ordenar los parámetros de OAuth alfabéticamente por clave
      const sortedParameters = Object.keys(oauthParameters)
          .sort()
          .map((key) => `${key}=${encodeURIComponent(oauthParameters[key])}`)
          .join('&');

      console.log('sor', sortedParameters)
      //   // Crear la cadena base para la firma
      const signatureBaseString = `GET&${encodeURIComponent(baseUrl)}&${encodeURIComponent(sortedParameters)}`;

      //   // Crear la clave de firma
      const signingKey = `${process.env.CONSUMER_SECRET}&${process.env.ACCESS_TOKEN_SECRET}`;

      //   // Calcular la firma HMAC-SHA256
      const hmac = crypto.createHmac('sha256', signingKey);
      hmac.update(signatureBaseString);
      const oauthSignature = hmac.digest('base64');

      //   // Construir la cabecera de autorización

      const authorizationHeader = `OAuth realm="${realm}", oauth_consumer_key="${process.env.CONSUMER_KEY}", oauth_token="${process.env.ACCESS_TOKEN}", oauth_nonce="${oauthNonce}", oauth_timestamp="${oauthTimestamp}", oauth_signature="${oauthSignature}", oauth_signature_method="${oauthSignatureMethod}", oauth_version="${oauthVersion}"`;

      //   // Configuración de la solicitud Axios
      console.log('authorizationHeader', authorizationHeader)
      const axiosConfig: AxiosRequestConfig = {
          method: 'get',
          url: baseUrl, 
          headers: {
              'Prefer': 'transient',
              'Content-Type': 'application/json',
              'Authorization': authorizationHeader,
          },
      };

      try {
          const response = await axios(axiosConfig);

          return {
              "PRO":response.data.custentity_ks_empleado_proveedor_employe.refName.split(' ')[0].split('-')[1],
              "CLI":response.data.custentitycustentity_ks_empleado_cliente.refName.split(' ')[0].split('-')[1],


              };
      } catch (error) {
          console.error('error', error);
          console.error('error', error['response']['data']['o:errorDetails']);

          throw error;
      }
    }

我认为这个问题可能与 OAuth 1.0 身份验证或我如何生成签名有关。对于解决这个问题有什么建议吗?我可以向不带参数的端点发出请求并且它可以工作,但我需要过滤结果。

javascript node.js netsuite netsuite-rest-api
2个回答
0
投票

检查你的基本网址,你有一个额外的大括号


0
投票

你有没有弄清楚这一点?我陷入了完全相同的问题。 Postman 请求每次都有效,但相同的请求(使用 RestSharp)失败并显示 401。

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