Request API node js using bearer token
Asked Answered
K

1

8

I tried doing the following:

request({
   url: 'https://vdms-dev.clientsolve.com/evoDMDev/api_event.php',
   headers: {
      'Authorization': 'Bearer 71D50F9987529'
   }
}, function(err, res) {
       console.log(res);
});

The log is showing undefined but when I try it on Postman it seems to be working fine.

Any help would be appreciated!

Kenner answered 18/5, 2018 at 3:10 Comment(2)
Has the package documentation not helped? npmjs.com/package/express-bearer-token github.com/auth0/node-jsonwebtokenJulejulee
I would advise you to try Passport.js. There is node module to support Bearer token. See passportjs.org/docs/oauth2-apiPaver
R
9

Since your are calling https host (https://evodms-dev.clientsolve.com/evoDMSDev/api/api_event_all.php), request client will throws an error while doing SSL handshake, thats why you got response as undefined. Inorder to check the exact error response log the error console.error("Error Response : ", err)

Checkout this working snippet with error handling.err

Note: Now you will get Invalid Bearer Token error, Enter valid Bearer token

const request = require('request');

request({
  url: 'https://evodms-dev.clientsolve.com/evoDMSDev/api/api_event_all.php',
  headers: {
     'Authorization': 'Bearer 71D50F9987529'
  },
  rejectUnauthorized: false
}, function(err, res) {
      if(err) {
        console.error(err);
      } else {
        console.log(res.body);
      }

});
Rubyeruch answered 18/5, 2018 at 7:22 Comment(2)
that only seems to get the request from the https url, How to get it to work on localhost:3000?Indira
Use localhost:3000, In case of any issues kindly detail your issues.Rubyeruch

© 2022 - 2024 — McMap. All rights reserved.