Flic Home

    Community

    • Login
    • Search
    • Popular
    • Users

    Help Needed: Resolving 401 Error with HTTP Requests in Flic Hub SDK

    Flic Hub SDK
    2
    4
    699
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • flic 16
      flic 16 last edited by

      Hello Community,

      I'm currently working with the Flic Hub SDK and facing an issue with making authenticated HTTP requests to the SMS Burst API. Despite the script working correctly on my local Node.js environment, I consistently receive a 401 Unauthorized error when running it on the Flic Hub.

      Here's a brief overview of what I'm trying to achieve and the steps I've taken so far:

      Goal:

      To make an authenticated GET request to the SMS Burst API from the Flic Hub SDK environment.

      Approach:

      I am using Basic Authentication, encoding the API key and secret in Base64 and setting it in the Authorization header.

      Here's a snippet of the script I'm using which does not work:

      const http = require('http');
      
      const burstAPI_Key = 'api_key_here';
      const burstAPI_Secret = 'API_secret_here!'; 
      
      // Headers with API key and secret key
      const headers = {
          "x-api-key": burstAPI_Key,
          "x-secret-key": burstAPI_Secret
      };
      
      // Making the HTTP request
      function checkSmsBurstApi() {
          http.makeRequest({
              url: 'https://api.transmitsms.com/get-lists.json',
              method: 'GET',
              headers: headers
          }, function(err, res) {
              if (err) {
                  console.error("An error occurred:", err);
              } else {
                  console.log("Response status code:", res.statusCode);
                  // Optionally, log the response body or headers here for more information
              }
          });
      }
      
      // Execute the function to check the API
      checkSmsBurstApi();
      

      Issue:

      The script works fine locally on my Node.js setup, but in the Flic Hub SDK, it results in a 401 error.

      I've double-checked the API credentials, and they are correct.

      Questions:

      Are there known issues or specific requirements for making authenticated HTTP requests in the Flic Hub SDK environment?

      Does the Flic Hub SDK handle HTTP headers differently that might affect Basic Authentication?

      Any suggestions or alternative methods that could help resolve this issue?

      I appreciate any insights or advice you can offer. Thank you in advance for your help!

      Emil 1 Reply Last reply Reply Quote 0
      • Emil
        Emil FlicTeam @flic 16 last edited by

        @flic-16 Can you try this base64 encode method instead:

        function base64Encode(str) {
            return Duktape.enc('base64', str);
        }
        
        1 Reply Last reply Reply Quote 0
        • flic 16
          flic 16 @Emil last edited by

          @Emil ```
          const http = require('http');

          const burstAPI_Key = 'api_key_here';
          const burstAPI_Secret = 'API_secret_here;

          // Function to encode credentials to Base64 for Basic Authentication
          function base64Encode(str) {
          return Buffer.from(str).toString('base64');
          }

          // Forming the Authorization header
          const authHeader = 'Basic ' + base64Encode(burstAPI_Key + ':' + burstAPI_Secret);

          // Making the HTTP request
          function checkSmsBurstApi() {
          http.makeRequest({
          url: 'https://api.transmitsms.com/get-lists.json',
          method: 'GET',
          headers: {
          "Authorization": authHeader
          }
          }, function(err, res) {
          if (err) {
          console.error("An error occurred:", err);
          } else {
          console.log("Response status code:", res.statusCode);
          // Optionally, log the response body or headers here for more information
          }
          });
          }

          // Execute the function to check the API
          checkSmsBurstApi();

          The above code results in this error:

          TypeError: undefined not callable (property 'from' of [object Function])
          at [anon] (duk_js_call.c:2917) internal
          at base64Encode (root/basicBurstSMSTest/main.js:8)
          at [anon] (root/basicBurstSMSTest/main.js:12)
          at require (init.js:131)
          at [anon] (init.js:139) preventsyield
          at runInit () native strict preventsyield
          at handlePacket (pipe_communication.js:48)
          at readCallback (pipe_communication.js:93) preventsyield

          Thanks for the ongoing support.

          Emil 1 Reply Last reply Reply Quote 0
          • Emil
            Emil FlicTeam @flic 16 last edited by

            @flic-16 You say that you use Basic Authentication, but I don't see where you are setting the Authorization header to "Basic " followed by the base64 encoding of username:password. Instead you are setting some custom x-api-key/x-secret-key headers. Which authentication method should you actually use?

            flic 16 1 Reply Last reply Reply Quote 0
            • First post
              Last post