Bandwidth / node-numbers

Node SDK for Bandwidth Numbers
https://dev.bandwidth.com
MIT License
1 stars 5 forks source link

Some requests throw 401 #64

Open yulierbrainhi opened 10 months ago

yulierbrainhi commented 10 months ago

Checklist

Description

In nodejs when I call some functions it throws a 401 error

Environment Information

Sample Code Snippet

var numbers = require("@bandwidth/numbers");

var client = new numbers.Client("******", "*******", "********");

numbers.AvailableNumbers.list(client, function (err, sites) {
    console.log(err, sites)
});

Stack Trace

Error: Unauthorized
    at Request.callback (/.../node_modules/superagent/lib/node/index.js:706:15)
    at IncomingMessage.<anonymous> (/.../node_modules/superagent/lib/node/index.js:916:18)
    at IncomingMessage.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1589:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  status: 401,
  response: <ref *1> Response {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    res: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: [TLSSocket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      rawHeaders: [Array],
      rawTrailers: [],
      joinDuplicateHeaders: undefined,
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 401,
      statusMessage: '',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [ClientRequest],
      text: '',
      [Symbol(kCapture)]: false,
      [Symbol(kHeaders)]: [Object],
      [Symbol(kHeadersCount)]: 34,
      [Symbol(kTrailers)]: null,
      [Symbol(kTrailersCount)]: 0
    },
    request: Request {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      _agent: false,
      _formData: null,
      method: 'GET',
      url: 'https://dashboard.bandwidth.com/api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true',
      _header: [Object],
      header: [Object],
      writable: true,
      _redirects: 0,
      _maxRedirects: 5,
      cookies: '',
      qs: {},
      _query: [],
      qsRaw: [],
      _redirectList: [],
      _streamRequest: false,
      _buffer: true,
      req: [ClientRequest],
      protocol: 'https:',
      host: 'dashboard.bandwidth.com',
      _endCalled: true,
      _callback: [Function (anonymous)],
      _fullfilledPromise: [Promise],
      res: [IncomingMessage],
      response: [Circular *1],
      called: true,
      [Symbol(kCapture)]: false
    },
    req: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'GET /api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true HTTP/1.1\r\n' +
        'Host: dashboard.bandwidth.com\r\n' +
        'Accept-Encoding: gzip, deflate\r\n' +
        'User-Agent: node-numbers\r\n' +
        'Authorization: Basic ......\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      joinDuplicateHeaders: undefined,
      path: '/api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'dashboard.bandwidth.com',
      protocol: 'https:',
      [Symbol(kCapture)]: false,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(errored)]: null,
      [Symbol(kHighWaterMark)]: 16384,
      [Symbol(kRejectNonStandardBodyWrites)]: false,
      [Symbol(kUniqueHeaders)]: null
    },
    text: '',
    body: {},
    files: undefined,
    buffered: true,
    headers: {
      'content-length': '0',
      connection: 'close',
      date: 'Wed, 20 Dec 2023 15:51:45 GMT',
      'www-authenticate': 'Basic realm="Bandwidth API"',
      'x-cache': 'Error from cloudfront',
      via: '1.1 077b94dab77b8114aebf503be197d7d8.cloudfront.net (CloudFront)',
      'x-amz-cf-pop': 'IAD89-C3',
      'x-amz-cf-id': 'cO-oUGEK3RAXOuow9dbFpqWUW4OtmLa2VzlSqUNbKhSm-h79ijjMDQ==',
      'x-xss-protection': '1; mode=block',
      'x-frame-options': 'DENY',
      'content-security-policy': "base-uri 'self' dashboard.bandwidth.com; object-src 'none'; script-src 'unsafe-inline' 'self' 'unsafe-eval' *.bandwidth.com dashboard.bandwidth.com *.sisense.com *.pendo.io; style-src 'unsafe-inline' 'self' 'unsafe-eval' *.bandwidth.com fonts.googleapis.com dashboard.bandwidth.com",
      'x-content-type-options': 'nosniff',
      'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
      'cache-control': 'no-store, no-cache',
      expires: '-1',
      pragma: 'no-cache',
      vary: 'Origin'
    },
    header: {
      'content-length': '0',
      connection: 'close',
      date: 'Wed, 20 Dec 2023 15:51:45 GMT',
      'www-authenticate': 'Basic realm="Bandwidth API"',
      'x-cache': 'Error from cloudfront',
      via: '1.1 077b94dab77b8114aebf503be197d7d8.cloudfront.net (CloudFront)',
      'x-amz-cf-pop': 'IAD89-C3',
      'x-amz-cf-id': 'cO-oUGEK3RAXOuow9dbFpqWUW4OtmLa2VzlSqUNbKhSm-h79ijjMDQ==',
      'x-xss-protection': '1; mode=block',
      'x-frame-options': 'DENY',
      'content-security-policy': "base-uri 'self' dashboard.bandwidth.com; object-src 'none'; script-src 'unsafe-inline' 'self' 'unsafe-eval' *.bandwidth.com dashboard.bandwidth.com *.sisense.com *.pendo.io; style-src 'unsafe-inline' 'self' 'unsafe-eval' *.bandwidth.com fonts.googleapis.com dashboard.bandwidth.com",
      'x-content-type-options': 'nosniff',
      'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
      'cache-control': 'no-store, no-cache',
      expires: '-1',
      pragma: 'no-cache',
      vary: 'Origin'
    },
    statusCode: 401,
    status: 401,
    statusType: 4,
    info: false,
    ok: false,
    redirect: false,
    clientError: true,
    serverError: false,
    error: Error: cannot GET /api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true (401)
        at Response.toError (/.../node_modules/superagent/lib/node/response.js:94:15)
        at ResponseBase._setStatusProperties (/.../node_modules/superagent/lib/response-base.js:123:16)
        at new Response (/.../node_modules/superagent/lib/node/response.js:41:8)
        at Request._emitResponse (/.../node_modules/superagent/lib/node/index.js:752:20)
        at IncomingMessage.<anonymous> (/.../node_modules/superagent/lib/node/index.js:916:38)
        at IncomingMessage.emit (node:events:526:35)
        at endReadableNT (node:internal/streams/readable:1589:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      status: 401,
      text: '',
      method: 'GET',
      path: '/api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true'
    },
    created: false,
    accepted: false,
    noContent: false,
    badRequest: false,
    unauthorized: true,
    notAcceptable: false,
    forbidden: false,
    notFound: false,
    unprocessableEntity: false,
    type: '',
    links: {},
    setEncoding: [Function: bound ],
    redirects: [],
    [Symbol(kCapture)]: false
  }
} 

Expected Behavior

List of available numbers

ajrice6713 commented 10 months ago

Hey @yulierbrainhi - is this on all requests or some? when you get a successful request are you utilizing the same client or a different one? Do you have an example of a successful request so that I can compare?

yulierbrainhi commented 10 months ago

Hello @ajrice6713, this is just for some requests. The most interesting thing is that this same request works well for me with Postman, but with Axios or the SDK from NodeJs it does not work for me.

This request works for me with the same username and password as the previous error: image

This previous request gives me an error with Axios and the SDK, but it works for me with Postman: image

Captura desde 2023-12-21 10-15-22

yulierbrainhi commented 10 months ago

After tracing the code I added this line and it works fine:

numbers.Client.globalOptions.apiEndPoint = "https://dashboard.bandwidth.com:443/v1.0";
ajrice6713 commented 10 months ago

Hey @yulierbrainhi - sorry for the delay as I have been on holiday

Glad that you found a workaround, my guess is there was an old API URL in there that was deprecated. I will get a ticket in to get this updated so that we ensure the client defaults to a working URL.

Thanks for raising this issue!

ajrice6713 commented 9 months ago

Hey @yulierbrainhi

Looking a bit deeper into your logs

    error: Error: cannot GET /api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true (401)
        at Response.toError (/.../node_modules/superagent/lib/node/response.js:94:15)
        at ResponseBase._setStatusProperties (/.../node_modules/superagent/lib/response-base.js:123:16)
        at new Response (/.../node_modules/superagent/lib/node/response.js:41:8)
        at Request._emitResponse (/.../node_modules/superagent/lib/node/index.js:752:20)
        at IncomingMessage.<anonymous> (/.../node_modules/superagent/lib/node/index.js:916:38)
        at IncomingMessage.emit (node:events:526:35)
        at endReadableNT (node:internal/streams/readable:1589:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      status: 401,
      text: '',
      method: 'GET',
      path: '/api/accounts//availableNumbers?xml2jsParserOptions%5BexplicitArray%5D=false&xml2jsParserOptions%5Basync%5D=true'
    },

It looks like the accountId path parameter was missing in the request error: Error: cannot GET /api/accounts//availableNumbers?, which would definitely cause a 401

I have been unable to recreate, and the v1 and port in the URL shouldn't matter - can you confirm your accountId was resolving?