Is the Nx Cloud up? Visit our Status Page for the current health and performance of the Nx Cloud.

Status Page

Can't fetch rest api call using with authorization(Bearer Token) headers from my local NX server. Please help!

Answered

Comments

10 comments

  • Andrey Terentyev
    • Network Optix team

    Hello,

    Could you please provide the following information: OS version, VMS build?

    Could you please elaborate on what is your objective and what are you doing to achieve it?

    As far as I understood the sceenshot, you're trying to launch a js script in the browser. Am I right? Could you share the code?

    0
  • Thar Nyi

    OS: Windows 10 Home x64
    VMS Build: Nx Meta v5.0.0.35744
    Setup: Currently, I installed Nx Meta v5.0.0.35744 Client & Server in my local. In my local Nx Meta Client, I’m using with cloud user account.
    Objective: I want to develop a livestream feature in my Next.js application. I’m trying to connect to the Nx Witness HTTP Stream API through token authentication. And, I want to access all owner API from Nx Meta through API call with bearer token in my Next.js application.
    My code screenshot:

    Thanks!

    0
  • Andrey Terentyev
    • Network Optix team

    Hi,

    Try running your server on another address than localhost.

    0
  • Thar Nyi

    Hi Andrey,

    Which one?

    1.

    2.

    0
  • Andrey Terentyev
    • Network Optix team

    I mean your server which runs node.js application.

    0
  • Thar Nyi

    Currently, I'm testing from next.js application.

    0
  • Andrey Terentyev
    • Network Optix team

    Hi,

    Could you create an example and share your source code as a text file, please?

    0
  • Thar Nyi

    Hi Andrey,

    const getAccessToken = async () => {

        await axios

          .get(`https://localhost:7001/api/ping`)

          .then(async (response) => {

            console.log(response)

            if (response.status === 200) {

              const { data: response } = await axios.get(

                `https://localhost:7001/api/moduleInformation?allModules=true`

              )

              const cloudSystemId = response.reply[0].cloudSystemId

              console.log('cloudSystemId: ', response.reply[0].cloudSystemId)

              if (cloudSystemId) {

                const data = {

                  username: 'username',

                  password: 'password',

                  setCookie: true,

                }

                const headersConfig = {

                  headers: {

                    'Content-Type': 'application/json',

                    'Access-Control-Allow-Origin': '*',

                  },

                }

                const { data: response } = await axios.post(

                  `https://localhost:7001/rest/v1/login/sessions`,

                  data,

                  headersConfig

                )

                const accessToken = `Bearer ${response.token}`

                console.log('accessToken: ', accessToken)

                getDeviceRecords(accessToken)

              }

            } else {

            }

          })

          .catch(function (error) {

            console.log(error)

          })

          .then(function () {

            console.log('End.')

          })

      }

      const getDeviceRecords = async (accessToken) => {

        const { data: response } = await axios.get(

          `https://localhost:7001/rest/v1/devices`,

          {

            headers: {

              Authorization: accessToken,

            },

          }

        )

        console.log('getDeviceRecords: ', response)

      }

    I already tested with this article <https://support.networkoptix.com/hc/en-us/articles/360016266074-Cloud-API-Route-API-Calls-via-Nx-Cloud>. It works for me. But, I want to create login session with local user in my local system. When I'm testing with local user and nx witness local server(default:; https://localhost:7001), I can't call API.

    Thanks.

    0
  • Andrey Terentyev
    • Network Optix team

    Hello,

    By default, POST will never work in the frontend code in the browser.
    Consider Invoking the POST APIs from the backend of your web service.
    The worrflow would be the following.

    1. The code in the browser requests your web service backend.
    2. Your web service backend calls the Server POST API
    3. Your web service backend returns data to the frontend code.
    0
  • Thar Nyi

    Thanks, Andrey

    0

Please sign in to leave a comment.