【问题标题】:How to add Bearer token to requestify post method in nodejs?如何在 nodejs 中添加 Bearer 令牌以请求 post 方法?
【发布时间】:2019-05-29 19:56:18
【问题描述】:

如何使用 post 方法传递 Bearer 令牌。我尝试使用邮递员,但收到此响应“错误:未经授权的访问。请求未授权”

            await turnContext.sendActivity(`${await requestify.request(url, {
                method: 'POST',
                body: data,
                dataType: 'json',
                auth:{
                    "Bearer":access_token // token
                }
            }).then(async function (res) {
                console.log(res.body);
                return res.body;
            })}`);

【问题讨论】:

    标签: javascript node.js requestify


    【解决方案1】:

    您需要在令牌中添加Bearer 作为前缀:

     await turnContext.sendActivity(`${await requestify.request(url, {
                    method: 'POST',
                    body: data,
                    dataType: 'json',
                    auth:{
                        `Bearer ${access_token}` // token
                    }
                }).then(async function (res) {
                    console.log(res.body);
                    return res.body;
                })}`);
    

    【讨论】:

      【解决方案2】:

      查看documentation auth 属性仅用于基本身份验证,因此只需手动添加授权标头

      await requestify.request(url, {
          method: 'POST',
          body: data,
          dataType: 'json',
          headers :{
              Authorization:"Bearer " + access_token // token
          }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 2017-11-04
        • 2019-12-22
        • 1970-01-01
        • 2020-06-01
        • 1970-01-01
        相关资源
        最近更新 更多