# Fetch app's roles from within a rule

**URL:** <https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818>\
**Category:** Get Help\
**Tags:** api, rules, management-api, auth0\
**Created:** [July 8, 2021, 3:29pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818 "2021-07-08T15:29:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![royibernthal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/royibernthal/32/16678_2.png) [@royibernthal](https://community.auth0.com/u/royibernthal)\
**Post date:** [July 8, 2021, 3:29pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818/1 "2021-07-08T15:29:04Z")

</div>

Hi,

I’d like to fetch the app’s roles list (not the specific user’s) from Auth0 inside of a rule.

I can’t access ManagementClient/getRoles with auth0.accessToken because it’s limited to the read:users and update:users scopes.

This information is based on both tests and the following article:

> **[Use the Management API from within Rules](https://auth0.com/docs/customize/rules/use-management-api)**
>
> Learn how to use the Management API from within rules.

I’m trying to get a credentials grant with the ‘read:roles’ scope, but I’m getting an access denied error.

**Rule:**

```auto
function (user, context, callback) {
  const lib = require('auth0@2.23.0');
  
  function getToken() {
    const auth = new lib.AuthenticationClient({
      domain: auth0.domain,
      clientId: 'myclientid',
      clientSecret: 'myclientsecret'
    });

    return auth.clientCredentialsGrant(
      {
        audience: 'https://mytenant.auth0.com/api/v2/',
        scope: 'read:roles'
      }
    );
  }

	getToken()
  	.then(result => {
    	console.log('result', result);
  	})
  	.catch(error => {
    	console.log('error', error);
  	})
  	.finally(() => {
    	callback(null, user, context);
  	});
}

```

**Error:**

```auto
access_denied: {"error":"access_denied","error_description":"Client is not authorized to access \"https://royibernthal.us.auth0.com/api/v2/\". You might probably want to create a \"client-grant\" associated to this API. See: https://auth0.com/docs/api/v2#!/Client_Grants/post_client_grants"}
    at /data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/src/Client.js:387:27
    at Request.callback (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/index.js:728:3)
    at /data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/index.js:916:18
    at Stream.<anonymous> (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/parsers/json.js:19:7)
    at Stream.emit (events.js:314:20)
    at Stream.EventEmitter.emit (domain.js:506:15)
    at Unzip.<anonymous> (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/unzip.js:55:12)
    at Unzip.emit (events.js:326:22)
    at Unzip.EventEmitter.emit (domain.js:506:15)
    at endReadableNT (_stream_readable.js:1241:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  statusCode: 403,
  requestInfo: {
    method: 'post',
    url: 'https://royibernthal.us.auth0.com/oauth/token'
  },
  originalError: Error: Forbidden
      at Request.callback (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/index.js:706:15)
      at /data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/index.js:916:18
      at Stream.<anonymous> (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/parsers/json.js:19:7)
      at Stream.emit (events.js:314:20)
      at Stream.EventEmitter.emit (domain.js:506:15)
      at Unzip.<anonymous> (/data/_verquire/_node12/auth0-extension-s3-tools/1.1.1/node_modules/rest-facade/node_modules/superagent/lib/node/unzip.js:55:12)
      at Unzip.emit (events.js:326:22)
      at Unzip.EventEmitter.emit (domain.js:506:15)
      at endReadableNT (_stream_readable.js:1241:12)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    status: 403,
    response: Response {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      res: [IncomingMessage],
      request: [Request],
      req: [ClientRequest],
      text: '{"error":"access_denied","error_description":"Client is not authorized to access \\"https://royibernthal.us.auth0.com/api/v2/\\". You might probably want to create a \\"client-grant\\" associated to this API. See: https://auth0.com/docs/api/v2#!/Client_Grants/post_client_grants"}',
      body: [Object],
      files: undefined,
      buffered: true,
      headers: [Object],
      header: [Object],
      statusCode: 403,
      status: 403,
      statusType: 4,
      info: false,
      ok: false,
      redirect: false,
      clientError: true,
      serverError: false,
      error: [Error],
      created: false,
      accepted: false,
      noContent: false,
      badRequest: false,
      unauthorized: false,
      notAcceptable: false,
      forbidden: true,
      notFound: false,
      unprocessableEntity: false,
      type: 'application/json',
      links: {},
      setEncoding: [Function: bound],
      redirects: [],
      [Symbol(kCapture)]: false
    }
  }
}

```

---

<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:** [July 9, 2021, 9:33pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818/3 "2021-07-09T21:33:12Z")

</div>

Hi @royibernthal,

Welcome to the Community!

In order to authorize your client, you will need to navigate to the Management API settings in your dashboard and toggle the application in the ‘Authorized’ position.

 ![Screen Shot 2021-07-09 at 2.31.13 PM](https://global.discourse-cdn.com/auth0/original/3X/0/2/0208b305a89b715eae80464f12d53a0e5087dfc3.png)

**Note: Be aware of the management API rate limits, calling the management API on every authentication can quickly run you into the limits.**

---

<div class="post-metadata">

**Author:** ![royibernthal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/royibernthal/32/16678_2.png) [@royibernthal](https://community.auth0.com/u/royibernthal)\
**Post date:** [July 9, 2021, 11:27pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818/4 "2021-07-09T23:27:39Z")

</div>

Perfect, thanks 🙂

Regarding the managment API rate limits - how would I return the up-to-date Auth0 list of roles in every response (doing an integration with Hasura) without running into the limits?

Would I have to implement some caching on my own servers and access it from within the rule or is there a better way to do it?

---

<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:** [July 12, 2021, 6:46pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818/5 "2021-07-12T18:46:58Z")

</div>

Check out this doc that has an example of how to add roles to the token:

> **[Sample Use Cases: Rules with Authorization](https://auth0.com/docs/manage-users/access-control/sample-use-cases-rules-with-authorization)**
>
> Learn how to use rules with roles-based access control (RBAC). For use with our Authorization Core feature set.

---

<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 27, 2021, 6:47pm UTC](https://community.auth0.com/t/fetch-apps-roles-from-within-a-rule/65818/6 "2021-07-27T18:47:22Z")

</div>

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