# Invalid Credentials Error when calling exchange\_code\_for\_token

**URL:** <https://forums.nylas.com/t/invalid-credentials-error-when-calling-exchange-code-for-token/418>\
**Category:** Question and Answers\
**Tags:** authentication\
**Created:** [September 5, 2024, 11:39pm UTC](https://forums.nylas.com/t/invalid-credentials-error-when-calling-exchange-code-for-token/418 "2024-09-05T23:39:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jose](https://forums.nylas.com/u/jose)\
**Post date:** [September 5, 2024, 11:39pm UTC](https://forums.nylas.com/t/invalid-credentials-error-when-calling-exchange-code-for-token/418/1 "2024-09-05T23:39:37Z")

</div>

I’m migrating my application from v2 to v3, I set a sandbox application to test my changes and I’m getting this error when calling the `exchange_code_for_token` method in the Python SDK, my code looks like this:

```python
code_exchange_request = CodeExchangeRequest(
     code=code,
     redirect_uri=redirect_uri,
     client_id=settings.NYLAS_CLIENT_ID,
     client_secret=settings.NYLAS_CLIENT_SECRET,
)

try:
    token: CodeExchangeResponse = self.client.auth.exchange_code_for_token(code_exchange_request)
except NylasOAuthError as e:
    log.logger.error(f"[NYLAS] Error {e.error_code} {e.error_description}.")
    raise e

```

The output in the logs looks like this: `ERROR [NYLAS] Error 25022 Invalid credentials provided.`

I tried using my client\_id and client\_secret in the Nylas web API client from [here](https://developer.nylas.com/docs/api/v3/admin/#post-/v3/connect/token) and I got the same error:

```auto
{
  "error": "invalid_request_error",
  "error_code": 401,
  "error_description": "request is empty or malformed",
  "error_uri": "https://accounts.nylas.io/#tag/Event-Codes",
  "request_id": "e774609a-eb1a-44d3-abe7-faf04254c6c8"
}

```

What could I be doing wrong?

---

<div class="post-metadata">

**Author:** ![Blag](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.nylas.com/blag/32/9_2.png) [@Blag](https://forums.nylas.com/u/Blag)\
**Post date:** [September 6, 2024, 1:13am UTC](https://forums.nylas.com/t/invalid-credentials-error-when-calling-exchange-code-for-token/418/2 "2024-09-06T01:13:42Z")

</div>

Hello @jose Let me take a look and will get back to you…I will need to run some tests…
