# Access\_token is malformed jwt when authenticating with google-oauth2

**URL:** <https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218>\
**Category:** Get Help\
**Tags:** nodejs, sso-integration, jwt-validation, authentication-api\
**Created:** [December 3, 2021, 4:57pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218 "2021-12-03T16:57:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattymillar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattymillar/32/33455_2.png) [@mattymillar](https://community.auth0.com/u/mattymillar)\
**Post date:** [December 3, 2021, 4:57pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/1 "2021-12-03T16:57:46Z")

</div>

I have a web application and I authenticate with google.  
I get a code back and I exchange it for a token by POST to /oauth/token.  
I get a successful token response, but when I try decode the access\_token inside I get “jwt malformed”.

```auto
const url = `https://${process.env.DOMAIN}/oauth/token`

const data = {
      grant_type: 'authorization_code',
      client_id: process.env.CLIENT_ID,
      client_secret: process.env.CLIENT_SECRET,
      code: "the code",
      redirect_uri: process.env.CALLBACK_URL,
}

const resp = await axios.post(url, qs.stringify(data))

```

I have also tried sending the data as json with the same result.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [December 3, 2021, 7:51pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/3 "2021-12-03T19:51:33Z")

</div>

Hi @mattymillar,

Thanks for reaching out to the Auth0 Community!

I understand that you’ve received a malformed access token when authenticating with google-oauth2.

After looking at your `axios.post` request, it appears to be missing the request headers.

Could you try the following instead:

```auto
const url = `https://${process.env.DOMAIN}/oauth/token`

const data = {
    grant_type: 'authorization_code',
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    code: "the code",
    redirect_uri: process.env.CALLBACK_URL,
}

const options = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
};

const resp = await axios.post(url, qs.stringify(data), options)

```

The [Call Your API Using the Authorization Code Flow](https://auth0.com/docs/authorization/flows/call-your-api-using-the-authorization-code-flow#request-tokens) documentation goes into full detail on how to form the `/oauth/token` request.

After doing so, you should be able to decode your access token without issues.

Please feel free to reach out if you have any further questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![mattymillar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattymillar/32/33455_2.png) [@mattymillar](https://community.auth0.com/u/mattymillar)\
**Post date:** [December 4, 2021, 10:46am UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/4 "2021-12-04T10:46:27Z")

</div>

Hi @rueben.tiow,

Thanks for your reply!

Unfortunately I don’t think this is the issue… If you send a request via axios.post with a query string as data it automatically sets the content-type header to application/x-www-form-urlencoded.

You can see it in the axios docs [here](https://axios-http.com/docs/urlencoded). I confirmed this by sending the request to my own endpoint and checking the headers.

I tried setting the header explicitly as in your example code but I get the same result.

I also tried using curl with the same result

```auto
curl --request POST \
  --url 'https://***.eu.auth0.com/oauth/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'grant_type=authorization_code' \
  --data 'client_id=***' \
  --data 'client_secret=***' \
  --data 'code=***' \
  --data 'redirect_uri=http://localhost:3000/callback'

```

If you’ve got any other ideas they’d be much appreciated!

Cheers,

Matt

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [December 6, 2021, 2:20pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/5 "2021-12-06T14:20:31Z")

</div>

Hi Matt,

You are getting an opaque access token. You must specify an audience to get a JWT access token.

John

---

<div class="post-metadata">

**Author:** ![mattymillar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattymillar/32/33455_2.png) [@mattymillar](https://community.auth0.com/u/mattymillar)\
**Post date:** [December 6, 2021, 2:43pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/6 "2021-12-06T14:43:01Z")

</div>

Hi @john.gateley

Thanks for your reply! I tried curl with the audience parameter included but I get the same result…

```auto
curl --request POST \
  --url 'https://***.eu.auth0.com/oauth/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'grant_type=authorization_code' \
  --data 'client_id=***' \
  --data 'client_secret=***' \
  --data 'code=***' \
  --data 'redirect_uri=http://localhost:3000/callback' \
  --data 'audience=https://***/'

```

I will do some digging into opaque tokens!

Cheers,

Matt

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [December 6, 2021, 3:09pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/7 "2021-12-06T15:09:01Z")

</div>

Thanks for helping out on this one @john.gateley.

Hi @mattymillar,

You’ll need to take one step back to your `/authorize` request and make sure that you’ve included the `audience` parameter.

For example:

```auto
https://YOUR_DOMAIN/authorize?
    response_type=code&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    scope=SCOPE&
    audience=API_AUDIENCE&
    state=STATE

```

Moreover, I’d like to reiterate that the access token is opaque because there is no value passed for the `audience` parameter. This is to be expected and explained in our [Get Access Tokens](https://auth0.com/docs/security/tokens/access-tokens/get-access-tokens#control-access-token-audience) docs. Using your API identifier or the Management API identifier will produce a JWT access token instead.

Please let me know us if there’s anything else we can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![mattymillar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattymillar/32/33455_2.png) [@mattymillar](https://community.auth0.com/u/mattymillar)\
**Post date:** [December 6, 2021, 5:24pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/8 "2021-12-06T17:24:09Z")

</div>

@rueben.tiow @john.gateley

Thank you! This has solved my issue.

Cheers

Matt

---

<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:** [December 21, 2021, 5:25pm UTC](https://community.auth0.com/t/access-token-is-malformed-jwt-when-authenticating-with-google-oauth2/74218/9 "2021-12-21T17:25:04Z")

</div>

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