# Management API throwing Unauthorized: Invalid Token error when attempting to create Password Change Ticket

**URL:** <https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449>\
**Category:** Get Help\
**Tags:** emails, management-api\
**Created:** [July 17, 2023, 8:30pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449 "2023-07-17T20:30:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![slcredentialmanager](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/slcredentialmanager/32/43984_2.png) [@slcredentialmanager](https://community.auth0.com/u/slcredentialmanager)\
**Post date:** [July 17, 2023, 8:30pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/1 "2023-07-17T20:30:53Z")

</div>

Problem described in title. Here is my code:

```auto
var request = require("request");

var options = { 
  method: 'POST',
  url: `https://${MGMT_DOMAIN}/api/v2/tickets/password-change`,
  headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${access_token}` },
  body: `{"connection_id": "${connection_id}", "email": "${email}"}` 
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

```

I’ve double checked all of the variables (MGMT\_DOMAIN, access\_token, connection\_id). I don’t know what I’m doing wrong. I’ve also tried using the nodejs ManagementClient api instead, with the same error:

```auto
const management = new ManagementClient({
    domain: MGMT_DOMAIN,
    token: access_token
});

management.createPasswordChangeTicket({
  email,
  connection_id
})
  .then(console.log)
  .catch(console.error)

```

I’ve also tried without directly specifying the token, using:

```auto
const management = new ManagementClient({
    domain: MGMT_DOMAIN,
    clientId: MGMT_ID,
    clientSecret: MGMT_SECRET
});

```

with the same error. The API I am using is the default Auth0 Management API, so I am not sure what I’m doing wrong. I know I haven’t messed up the secrets/ids because I can call `management.getAccessToken()` just fine.

Edit: I managed to get it to work with the access token, but I need it to work with just providing the clientId and clientSecret and domain, so that I can use it with Auth0 Actions

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [July 17, 2023, 8:49pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/2 "2023-07-17T20:49:43Z")

</div>

Hey @slcredentialmanager you could try setting an access token and testing the Management API endpoint [here](https://auth0.com/docs/api/management/v2/tickets/post-password-change). Do you get the same error? Does your M2M application have `create:user_tickets` permissions?

---

<div class="post-metadata">

**Author:** ![slcredentialmanager](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/slcredentialmanager/32/43984_2.png) [@slcredentialmanager](https://community.auth0.com/u/slcredentialmanager)\
**Post date:** [July 17, 2023, 8:52pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/3 "2023-07-17T20:52:04Z")

</div>

I just updated the question description. I think the access token I was using was incorrect. I’ve fixed it now. However, the ideal goal would be to get things working with the ManagementClient object using just the client\_id and client\_secret and domain parameters. That way, I can provide those in the secrets of an Auth0 Action, whereas the token is so long that I can’t store it as a secret

---

<div class="post-metadata">

**Author:** ![slcredentialmanager](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/slcredentialmanager/32/43984_2.png) [@slcredentialmanager](https://community.auth0.com/u/slcredentialmanager)\
**Post date:** [July 17, 2023, 8:56pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/4 "2023-07-17T20:56:42Z")

</div>

Also, the token is only valid for 24 hours anyway, so I can’t just use it directly

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [July 17, 2023, 9:05pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/5 "2023-07-17T21:05:44Z")

</div>

Glad you got the access\_token error figured out @slcredentialmanager!

There is an Auth0 FAQ post [here](https://community.auth0.com/t/caching-management-api-access-tokens-in-login-action/95855) that shows how to generate access tokens using the clientId, clientSecret, and domain in an Auth0 Action, and how to cache it for future requests.

Hope this helps 🤞

---

<div class="post-metadata">

**Author:** ![slcredentialmanager](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/slcredentialmanager/32/43984_2.png) [@slcredentialmanager](https://community.auth0.com/u/slcredentialmanager)\
**Post date:** [July 17, 2023, 9:09pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/6 "2023-07-17T21:09:02Z")

</div>

Thanks. Unfortunately, I can’t seem to generate a valid token using the clientId, clientSecret and domain. I tried this:

```auto
const axios = require('axios').default;

const client_id = ...
const client_secret = ...
const MGMT_DOMAIN = ...

axios({
  method: 'POST',
  url: `https://${MGMT_DOMAIN}/oauth/token`,
  headers: { 'Content-Type': 'application/json' },
  data: {
    client_id: client_id,
    client_secret: client_secret,
    audience: `https://${MGMT_DOMAIN}/api/v2/`,
    grant_type: "client_credentials"
  }
})
  .then(res => {
    const token = res.data['access_token'];
    return axios({
      method: 'POST',
      url: `https://${MGMT_DOMAIN}/api/v2/tickets/password-change`,
      headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${token}`},
      data: { connection_id, email }
    })
  })
  .catch(console.error);

```

which basically gets an access token and then attempts to use it create a password change ticket. But I am still getting the same error, which is Unauthorized: Invalid Token

Edit: I also tried testing it out in [here](https://auth0.com/docs/api/management/v2/tickets/post-password-change), but it gave me the same error. Somehow, I am generating the token incorrectly

---

<div class="post-metadata">

**Author:** ![slcredentialmanager](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/slcredentialmanager/32/43984_2.png) [@slcredentialmanager](https://community.auth0.com/u/slcredentialmanager)\
**Post date:** [July 17, 2023, 9:26pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/7 "2023-07-17T21:26:24Z")

</div>

Ugh, it was a permissions issue. For some reason, the Auth0 Management API (Test Application) didn’t have all grants. I assigned all the grants to it, and it successfully makes the ticket.

However, no reset password email gets sent! No idea why.

Edit: Oops, I was supposed to use [this](https://auth0.com/docs/api/authentication#change-password) api call instead

---

<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:** [July 31, 2023, 9:27pm UTC](https://community.auth0.com/t/management-api-throwing-unauthorized-invalid-token-error-when-attempting-to-create-password-change-ticket/111449/8 "2023-07-31T21:27:19Z")

</div>

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