简体   繁体   English

如何使用 got.js 使用不记名令牌进行身份验证

[英]How to authenticate using bearer token using got.js

I'm trying to switch from request.js to got.js .我正在尝试从request.js切换到got.js I expect to see the got.js implementation authenticate similarly to how the request.js library does.我希望看到got.js实现以类似于request.js库的方式进行身份验证。 But, instead, I get the following error.但是,相反,我收到以下错误。

auth no longer supported.不再支持身份验证。 Replaced by username and password.替换为用户名和密码。

There is no mention of bearer tokens on the docs page.文档页面上没有提到不bearer令牌。

So how do I authenticate my request using bearer tokens using got.js ?那么如何使用got.js使用不记名令牌对我的请求进行身份验证? Or what am I doing wrong?或者我做错了什么?

Current code: request.js, working 当前代码:request.js,工作
const request = require('request'); const module.exports = config => { const options = { auth: { bearer: config.secret, }, }; const result = await new Promise(( resolve, reject, ) => { request.get( url, options, ( error, response, body, ) => { ...
New code: got.js, throws error 新代码:got.js,抛出错误
const got = require('got'); module.exports = async config => { const options = { auth: { bearer: config.secret, }, }; const result = await got(url, options); ... }

This should be worked, if I'm not wrong如果我没记错的话,这应该可行

const options = {
    headers: {
      'Authorization' : 'Bearer yourtoken'
    }
  };

worked for me !!为我工作!!

router.get('/product', (req,res)=>{
     const dataStream =  got.stream({
      url: "http://localhost:8000/products",
      method: "GET",
    
        hooks: {
        beforeRequest: [
            options => {
                var token= 'Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MTkzODA0NjIsImV4cCI6MTYxOTM4NDA2Mn0.JoJbRpPniGuMbwULEtts7I19QxEImvarT6AoqVuNb9w'
                options.headers['Authorization'] = token;

            }
        ]
    }
     });

pipeline(dataStream, res, (err) => {
      if (err) {
          console.log(err);
          res.sendStatus(500);
      }
  });
});
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Angular JS使用Authorization Bearer令牌从服务器获取图像 - How to get an Image from the Server using Authorization Bearer token using Angular JS 如何使用授权承载 + 令牌使用 Axios 注销用户 - How to log user out with Axios using authorization bearer + token 如何使用获取和不记名令牌连接到 API? - How can I connect to a API using fetch and bearer token? 如何使用AngularJS在浏览器cookie中存储身份验证承载令牌 - How to store authentication bearer token in browser cookie using AngularJS 在节点js中使用keycloak访问令牌验证rest api - Authenticate a rest api using keycloak access token in node js 在Swagge rUI中使用JavaScript传递承载令牌 - Passing bearer token using javascript in Swagge rUI 如何使用OAuth Node JS验证Angle 2 - How to authenticate angular 2 using oauth node js 是否可以在不使用 node.js 的情况下从 javascript 调用 login.microsoftonline.com 获取 Bearer Token - Is it possible to call login.microsoftonline.com from javascript to get Bearer Token without using node.js 如何使用 JS 验证 VSTS API PAT - How to authenticate VSTS APIs PAT using JS 通过令牌验证,然后通过会话验证令牌失败,使用Passport? - Authenticate by Token, then by Session if Token fails, using Passport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM