# Failed to get valid id\_token after code exchange

**URL:** <https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195>\
**Category:** Get Help\
**Tags:** id\_token\
**Created:** [April 12, 2017, 11:17am UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195 "2017-04-12T11:17:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Una\_Hou](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Una\_Hou](https://community.auth0.com/u/Una_Hou)\
**Post date:** [April 12, 2017, 11:17am UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/1 "2017-04-12T11:17:29Z")

</div>

Hi,

Our backend is php, the code to get id\_token is:

```
$auth0 = new Authentication(<domain>, <client_id>);
$oAuthClient = $auth0->get_oauth_client(<client_secret>, <redirect_uri>);

try {
    $oAuthClient->exchangeCode();
} catch (\Exception $ex) {

    return false; // return error
}
$idToken = $oAuthClient->getIdToken();

return json_encode("id_token" => $idToken]);

```

It worked fine before. We didn’t change code but somehow $idToken gets value ‘ **false** ’ now.

Here are some points may be relevant:

1. There is no error Exception thrown in code exchange.
2. In login url ($auth0-\>get\_authorize\_link()), we give **scope** containing ‘openid’ and **response\_type** = ‘code’.
3. Our connection is a custom database.
4. If I try with different domain/client/custom database connection, it works fine.

It looks like the difference is client/connection on Auth0 account side, but we don’t know what the exact cause is.

Any suggestions to find the cause would be appreciated!

  
  
Thank you,  
Una

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [April 12, 2017, 10:12pm UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/2 "2017-04-12T22:12:57Z")

</div>

Can you check if `exchangeCode` is throwing an exception and if it is, include the exception details in the question? You mention that you’re getting a value of `false` and your code is ignoring the exceptions thrown by that method and just returning `false` so this looks suspicious.

---

<div class="post-metadata">

**Author:** ![Una\_Hou](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Una\_Hou](https://community.auth0.com/u/Una_Hou)\
**Post date:** [April 12, 2017, 11:04pm UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/3 "2017-04-12T23:04:34Z")

</div>

Just made the code more complete according to your suspect.

Based on the code, we get {“id\_token”: false} as a result. If there is any Exception, it is supposed to get false directly.

---

<div class="post-metadata">

**Author:** ![Una\_Hou](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Una\_Hou](https://community.auth0.com/u/Una_Hou)\
**Post date:** [April 12, 2017, 11:04pm UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/4 "2017-04-12T23:04:34Z")

</div>

Just made the code more complete according to your suspect.

Based on the code, we get {“id\_token”: false} as a result. If there is any Exception, it is supposed to get false directly.

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [April 13, 2017, 10:23pm UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/5 "2017-04-13T22:23:32Z")

</div>

Thanks for the additional info; that likely implies that the code is being exchanged with success, but an [ID token is not being included](https://github.com/auth0/auth0-PHP/blob/f5960dffc746089cbddd42f537cc1f89b4f9b6eb/src/API/Oauth2Client.php#L282) in the response. One explanation could be that the `openid` scope was not being included, but you also state that you’re doing this. Can you try to reproduce this outside of your PHP app? For example, obtaining the code through a web browser and then exchange it using an HTTP client tool like Postman?

---

<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:** [March 2, 2018, 5:53am UTC](https://community.auth0.com/t/failed-to-get-valid-id-token-after-code-exchange/6195/6 "2018-03-02T05:53:13Z")

</div>


