最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Request-Promise throws "no auth mechanism defined" using asyncawait - Stack Overflow

programmeradmin0浏览0评论

I was just trying out async/await with request-promise and ran into this error:

RequestError: Error: no auth mechanism defined
      at new RequestError (node_modules/request-promise-core/lib/errors.js:14:15)
      at Request.plumbing.callback (node_modules/request-promise-core/lib/plumbing.js:87:29)
      at Request.RP$callback [as _callback] (node_modules/request-promise-core/lib/plumbing.js:46:31)
      at self.callback (node_modules/request/request.js:188:22)
      at Auth.onRequest (node_modules/request/lib/auth.js:133:18)
      at Request.auth (node_modules/request/request.js:1360:14)
      at Context.<anonymous> (test/routes.js:37:41)
  From previous event:
      at Request.plumbing.init (node_modules/request-promise-core/lib/plumbing.js:36:28)
      at Request.RP$initInterceptor [as init] (node_modules/request-promise-core/configure/request2.js:41:27)
      at new Request (node_modules/request/request.js:130:8)
      at request (node_modules/request/index.js:54:10)
      at Context.<anonymous> (test/routes.js:37:24)

It is an API endpoint that I built recently that's supposed to create a new user in MongoDB. It uses Basic Auth provided by Passport strategy, and I've tested with Postman that it works. I'm not exactly sure why this error is being thrown.

My request code (using Mocha):

it("creates a new user", async () => {
  const options = {
    method: "POST",
    uri: `http://localhost:${process.env.PORT}/api/users`,
    headers: {
      "User-Agent": "Request-Promise",
      "Content-Type": "application/json"
    },
    body: {
      email: "[email protected]",
      password: "password",
      firstName: "John",
      lastName: "Smith"
    },
    json: true
  };
  const resp = await request(options).auth(APP_ID, SIGNATURE, false);
  expect(resp).to.be.an("object");
});

Edit: I should probably also add that I'm using node 8.2.1 and npm 5.3.0.

I was just trying out async/await with request-promise and ran into this error:

RequestError: Error: no auth mechanism defined
      at new RequestError (node_modules/request-promise-core/lib/errors.js:14:15)
      at Request.plumbing.callback (node_modules/request-promise-core/lib/plumbing.js:87:29)
      at Request.RP$callback [as _callback] (node_modules/request-promise-core/lib/plumbing.js:46:31)
      at self.callback (node_modules/request/request.js:188:22)
      at Auth.onRequest (node_modules/request/lib/auth.js:133:18)
      at Request.auth (node_modules/request/request.js:1360:14)
      at Context.<anonymous> (test/routes.js:37:41)
  From previous event:
      at Request.plumbing.init (node_modules/request-promise-core/lib/plumbing.js:36:28)
      at Request.RP$initInterceptor [as init] (node_modules/request-promise-core/configure/request2.js:41:27)
      at new Request (node_modules/request/request.js:130:8)
      at request (node_modules/request/index.js:54:10)
      at Context.<anonymous> (test/routes.js:37:24)

It is an API endpoint that I built recently that's supposed to create a new user in MongoDB. It uses Basic Auth provided by Passport strategy, and I've tested with Postman that it works. I'm not exactly sure why this error is being thrown.

My request code (using Mocha):

it("creates a new user", async () => {
  const options = {
    method: "POST",
    uri: `http://localhost:${process.env.PORT}/api/users`,
    headers: {
      "User-Agent": "Request-Promise",
      "Content-Type": "application/json"
    },
    body: {
      email: "[email protected]",
      password: "password",
      firstName: "John",
      lastName: "Smith"
    },
    json: true
  };
  const resp = await request(options).auth(APP_ID, SIGNATURE, false);
  expect(resp).to.be.an("object");
});

Edit: I should probably also add that I'm using node 8.2.1 and npm 5.3.0.

Share Improve this question edited Sep 15, 2017 at 15:39 spicypumpkin asked Aug 7, 2017 at 16:08 spicypumpkinspicypumpkin 1,2192 gold badges12 silver badges21 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Solved for me by changing from:

auth: { Bearer: token }

to:

auth: { bearer: token }

Note the case difference on 'bearer'.

This is usually caused by not providing suitable credentials. The code raising the error can be found here. Have you verified that APP_ID and SIGNATURE are not undefined in your test?

This solution works for me. I have needed to put the token inside the headers :

var rp = require('request-promise');

var uri = 'uri_for_my_post_request';
var token = 'access_token';
var body =  {
    title: 'My Title',
    content : 'My content'
}; 

var sendPost = async(my_uri,my_token,my_body)=>{
    var options = {
    method: 'POST',
    headers:{
        Authorization: ' Bearer ' + my_token           
   },
    uri: my_uri,
    body: my_body,
    json: true
};

const response = await rp(options);
console.log(response);
}

sendPost(uri,token,body);
发布评论

评论列表(0)

  1. 暂无评论