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

omtl5h9j  于 4个月前  发布在  Node.js
关注(0)|答案(2)|浏览(56)

我正在使用NetSuite API,并尝试使用查询参数检索员工,特别是“q”参数,遵循文档aqui。我使用以下端点:GET https://demo123.suitetalk.api.netsuite.com/services/rest/record/v1/customer?q=email START_WITH barbara。当我在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身份验证或我如何生成签名有关。有任何解决这个问题的建议吗?我可以向端点发出没有参数的请求,它可以工作,但我需要过滤结果。

nwsw7zdq

nwsw7zdq1#

我也遇到了和你一样的问题,经过不断尝试终于解决了。官网文档说这里要用空格显示,所以你需要把你加密查询参数中的空格替换成%20,再加密一次,输入auth,这样就过滤成功了,这很恶心,因为提示太少了。

g52tjvyc

g52tjvyc2#

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

相关问题