# Resource Owner Password Flow works fine with Postman but throws 401 Unauthorised with Axios Node.js

**URL:** <https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537>\
**Category:** Get Help\
**Tags:** management-api, login, auth0\
**Created:** [August 25, 2021, 2:08pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537 "2021-08-25T14:08:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 25, 2021, 2:08pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/1 "2021-08-25T14:08:05Z")

</div>

Hi,

**I browsed similar questions but no luck.**

We are building a SPA (Single Page App) which uses React.js on frontend and Node.js on backend and we want to authenticate our users upon successful registration.

For the registration, we are creating user accounts through our backend using **Auth0 management API** and that works perfectly fine but we are struggling with **Resource Owner Password Flow**.

When making API call from Postman, we successfully get the `access_token` , `refresh_token` , `id_token` , `token_type` , and `expires_in`.

Please see the screenshot of the API call using Postman.

 ![Screenshot 2021-08-25 at 6.47.17 PM](https://global.discourse-cdn.com/auth0/original/3X/9/e/9eb8e6fc9e1dd9b42ec808d6c9cbc67b9e247a11.png)

I just wrote a function to share here. Here’s my code:

```auto
require("dotenv").config();
const express = require("express");
const axios = require("axios").default;
const cors = require("cors");
const app = express();

const port = process.env.PORT || 4000;

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post("/login", (req, res) => {
  var options = {
    method: "POST",
    url: `https://${process.env.AUTH0_DOMAIN}/oauth/token`,
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: {
      grant_type: "password",
      username: req.body.username,
      password: req.body.password,
      audience: process.env.AUTH0_AUDIENCE,
      scope: "openid profile email",
      client_id: process.env.AUTH0_CLIENT_ID,
      client_secret: process.env.AUTH0_CLIENT_SECRET,
    },
  };

  axios(options)
    .then(function (response) {
      console.log(response.data);

      return res.status(200).json({
        status: "success",
        response: response.data,
      });
    })
    .catch(function (error) {
      console.error(error);
      return res.status(error.response.status).json({
        status: "error",
        response: error.response.data,
      });
    });
});

app.listen(port, () =>
  console.log(`Listing to server: http://localhost/${port}`)
);

```

**Our Requirement**

1. Ability to authenticate the users without redirecting the users to Auth0 hosted login page.
2. Ability to create user accounts through our backend using **Management API** and then using **Resource Owner Password Flow** to authenticate the users upon successful account creation through our backend.

Please help me out.

Thank you.

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 26, 2021, 5:27pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/2 "2021-08-26T17:27:03Z")

</div>

Is it possible for someone to help me out with this?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 26, 2021, 5:36pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/4 "2021-08-26T17:36:22Z")

</div>

Hi @assadullahch,

_Friendly tip: If you respond to your own post it removes it from the unanswered queue and it is likely to go unanswered._

As for you question, let’s see if we can figure it out 😄

Are you using the same client ID and secret for your postman app and your node app?

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 26, 2021, 5:38pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/5 "2021-08-26T17:38:53Z")

</div>

Hi @dan.woda ,

Sorry about responding to my question. We are on a tight deadline so.

I would appreciate your help.

I copied the client\_id straight from my text editor. Matched it many times and I can assure you that is correct. I am also not seeing logs related to this 401 response in Auth0 dashboard.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 26, 2021, 8:37pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/6 "2021-08-26T20:37:14Z")

</div>

Can you confirm the domain you are sending to is being input properly? I have seen cases where the domain env variable is causing issues, for instance:

```auto
https://${process.env.AUTH0_DOMAIN}/oauth/token

```

resolves as

```auto
https://https://your_domain/oauth/token

```

Also, you should ensure the application is authorized to access the audience you are passing.

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 27, 2021, 8:07am UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/7 "2021-08-27T08:07:01Z")

</div>

@dan.woda I removed the env variables and pasted the values directly in my code. Still there’s no luck.

Client id, domain and audience are the same exact I am using with Postman.

The strange thing is, there are no logs in Auth0 related to 401.

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 27, 2021, 8:35am UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/8 "2021-08-27T08:35:58Z")

</div>

In my Node.js Axios call, I commented out **audience** and **headers** and it’s working now.

You can see my Axios options here

```auto
const options = {
    method: "POST",
    url: `https://${process.env.AUTH0_DOMAIN}/oauth/token`,
    //headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: {
      grant_type: "password",
      username: req.body.username,
      password: req.body.password,
      //audience: process.env.AUTH0_AUDIENCE,
      scope: "openid profile email",
      client_id: process.env.AUTH0_CLIENT_ID,
      client_secret: process.env.AUTH0_CLIENT_SECRET,
    },
  };

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 30, 2021, 7:38pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/9 "2021-08-30T19:38:30Z")

</div>

If the `audience` param was causing the issue it is likely because the application (client ID) you are making the request with is not authorized to access that API (audience).

You can update this in settings of your API in the dashboard.

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 30, 2021, 7:47pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/10 "2021-08-30T19:47:46Z")

</div>

There was no issue with audience. I just commented out `headers` in my `Axios` call options and it all just started working as expected.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 30, 2021, 7:54pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/11 "2021-08-30T19:54:39Z")

</div>

Great let us know if you have any other questions.

---

<div class="post-metadata">

**Author:** ![assadullahch](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/assadullahch/32/30268_2.png) [@assadullahch](https://community.auth0.com/u/assadullahch)\
**Post date:** [August 30, 2021, 8:02pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/12 "2021-08-30T20:02:59Z")

</div>

I got another question. Please see this [here](https://community.auth0.com/t/is-registering-users-and-getting-user-meta-through-management-api-a-bad-practice/68672)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 30, 2021, 8:41pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/13 "2021-08-30T20:41:43Z")

</div>

I responded in that thread.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 14, 2021, 8:42pm UTC](https://community.auth0.com/t/resource-owner-password-flow-works-fine-with-postman-but-throws-401-unauthorised-with-axios-node-js/68537/14 "2021-09-14T20:42:40Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
