# "auth0-spa-js" get JWT for API access with correct audience

**URL:** <https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [July 18, 2023, 8:16am UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486 "2023-07-18T08:16:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.bjerge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/martin.bjerge/32/45180_2.png) [@martin.bjerge](https://community.auth0.com/u/martin.bjerge)\
**Post date:** [July 18, 2023, 8:16am UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/1 "2023-07-18T08:16:55Z")

</div>

I’m trying to have my Vue SPA to access my demo API.  
I have this up and running with a "Regular Web Application” see [Auth0-php regular web app + API - #13 by martin.bjerge](https://community.auth0.com/t/auth0-php-regular-web-app-api/108734/13)  
I have been reading this documentation [Call Your API from Your Single-Page App](https://auth0.com/docs/microsites/call-api/call-api-single-page-app)

In my main.js I initialize auth0:

```auto
// Install the authentication plugin here
const domain = AuthConfig.domain
const clientId = AuthConfig.clientId
Vue.use(Auth0Plugin, {
  domain,
  clientId,
  authorizationParams: {
    audience: 'https://api.example.com',
    redirect_uri: 'http://localhost:8080/'
  },
  onRedirectCallback: appState => {
    router.push(
      appState && appState.targetUrl
        ? appState.targetUrl
        : window.location.pathname
    )
  }
})

```

In my Home.vue component, I have a login button that works, and I can get all the user details.  
I then have a second button that should make a call to my API.

```auto
async api () {
      const idToken = await this.$auth.getIdTokenClaims()
      const accessToken = await this.$auth.getTokenSilently({ audience: 'https://api.example.com', detailedResponse: true })
      console.log('AccessToken:')
      console.log(accessToken)
      console.log('Bearer ' + accessToken.id_token)

      const result = await fetch('https://api.example.com/api/public', {
        method: 'GET',
        headers: {
          Authorization: 'Bearer ' + accessToken.id_token
        }
      })
      this.api_data = await result.text()
      // this.api_data = result
      console.log(this.api_data)
    },

```

All my code can be found here: [GitHub - martinbjerge/auth0\_simple\_api: Example of how to setup a simple API and protect it with AUTH0 and how to access it.](https://github.com/martinbjerge/auth0_simple_api)

Here the problem is that a “audience” value in the jwt (token) I get is not what I specified but the clientId of my SPA. Hence then I don’t get access to my API.

Any ideas to what I’m doing wrong here?

---

<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 20, 2023, 3:19pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/3 "2023-07-20T15:19:13Z")

</div>

Hi @martin.bjerge,

It looks like the token you are passing is the _ID Token_, not an _Access Token_. That would explain why it contains an `audience` claim with your client ID.

Our [Vue Quickstart](https://auth0.com/docs/quickstart/spa/vuejs/02-calling-an-api#retrieving-an-access-token) shows how to retrieve an Access Token.

Please let me know if you have any questions.

---

<div class="post-metadata">

**Author:** ![martin.bjerge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/martin.bjerge/32/45180_2.png) [@martin.bjerge](https://community.auth0.com/u/martin.bjerge)\
**Post date:** [July 20, 2023, 5:40pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/4 "2023-07-20T17:40:57Z")

</div>

Thank you very much for your input.

Sorry did not mention that I’m coding in Vue 2, as I also use bootstrap-vue which to my knowledge don’t work in Vue 3 yet.

So, I’m using @auth0/auth0-spa-js and I don’t see the function “getAccessTokenSilently” in that SDK

Can’t I do this in Vue 2?

---

<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 21, 2023, 2:04pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/5 "2023-07-21T14:04:15Z")

</div>

> [@martin.bjerge](#):
>
> So, I’m using @auth0/auth0-spa-js and I don’t see the function “getAccessTokenSilently” in that SDK

See[this method](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#get-access-token-with-no-interaction).

---

<div class="post-metadata">

**Author:** ![martin.bjerge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/martin.bjerge/32/45180_2.png) [@martin.bjerge](https://community.auth0.com/u/martin.bjerge)\
**Post date:** [July 26, 2023, 2:03pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/6 "2023-07-26T14:03:59Z")

</div>

Thanks, unfortunatly this did not work.

I have tried diferent options.

But i still get the SPA auduence with this and not the audience i provide in ConfAudience?

```auto
      const differentAudienceOptions = {
        authorizationParams: {
          audience: ConfAudience,
          scope: 'read:all',
          redirect_uri: 'http://localhost:8080'
        }
      }
      const accessToken = await this.$auth.getTokenWithPopup(differentAudienceOptions)

```

I have also update the code at github.

> <https://github.com/martinbjerge/auth0_simple_api/blob/main/frontend-vue2/src/views/Home.vue>

---

<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 26, 2023, 2:14pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/7 "2023-07-26T14:14:53Z")

</div>

Can you please show an example response you are seeing from the `getTokenSilently` method?

---

<div class="post-metadata">

**Author:** ![martin.bjerge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/martin.bjerge/32/45180_2.png) [@martin.bjerge](https://community.auth0.com/u/martin.bjerge)\
**Post date:** [July 27, 2023, 10:52am UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/9 "2023-07-27T10:52:41Z")

</div>

My BAD - Problem solved!

I have by mistake been looking at the id\_token and not the access\_token.

A bit og background:  
The access\_token was until i provided the updated audience not a JWT but an Opaque token.  
Hence to get a JWT, I accessed the id\_token as this is a JWT. But forgot to look back at the access\_token when I added the correct audience.

@dan.woda Thanks for your input and patience with me, the auth0 newbie 😊

Here is link til my github with the above example:

> **[GitHub - martinbjerge/auth0\_simple\_api: Example of how to setup a simple API...](https://github.com/martinbjerge/auth0_simple_api)**
>
> Example of how to setup a simple API and protect it with AUTH0 and how to access it. - GitHub - martinbjerge/auth0\_simple\_api: Example of how to setup a simple API and protect it with AUTH0 and how...

---

<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 27, 2023, 12:53pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/10 "2023-07-27T12:53:32Z")

</div>

Glad you got it figured out! Let us know if you have any other questions.

---

<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:** [August 10, 2023, 12:53pm UTC](https://community.auth0.com/t/auth0-spa-js-get-jwt-for-api-access-with-correct-audience/111486/11 "2023-08-10T12:53:51Z")

</div>

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